Free Python course. Sign Up for tracking progress →

Python: Objects

The data we work with in programs have important attributes. In Python, they're built right into the language. Data also have methods - functions within properties. Properties and methods are expressions, just like variables or function calls. All of these can be combined in different ways. We'll look at the basics of them in this lesson.

In programming, we operate with data, create numbers and strings, perform various operations on them and use their results. To perform an operation, we apply either operators or functions:

# Adding with the + operator
1 + 3 # 4

# Counting the length with the len() function
name = 'Hexlet'
len(name)  # 6

In the example above, there is a clear division: the data and functions are separated from each other. But this is not the only way to organize code. In Python, there's another approached used alongside this separation, the Object Oriented (OO) approach.

Object-oriented code is based on combining data and functions into one entity, an object. Data in this case are called attributes, and functions are called methods.

This is what it looks like:

name = 'Hexlet'
# upper() method
upper_name = name.upper()
print(upper_name)  # => 'HEXLET'

Strings in Python are objects. In the example above, we call a method, which is, a function associated with a string. The call is made using a period that comes right after the variable name. Otherwise, the methods work like normal functions.

The call can also be made directly:

'Hexlet'.upper()  # 'HEXLET'

There are many methods built into strings which developers need all the time. You can see a list of them in the documentation. Here are some useful examples:

name = 'Python'

# Returns the index of the first occurrence of a letter in a string
name.find('t')  # 2

# Changes to lower case
name.lower()  # 'python'

# Replaces one substring with another
name.replace('on', 'off')  # 'Pythoff'

The same goes for numbers and other data types that we've not looked at. You could say that in Python almost everything is an object:

x = -5
# Returns the modulus of a number
# The name looks strange, but it really is the name of the method
x.__abs__()

In the example above, there is a method name with two underscores at the beginning and at the end. In Python, this is the name given to methods that aren't intended to be called directly. Functions have been created for them that call methods themselves:

x = -5
abs(x)  # calls x.__abs__()
# -5 to the power of 3
pow(x, 3)  # calls x.__pow__(3)

The creator of Python decided that it would be clearer if mathematical operations and operations similar to mathematic ones were expressed in functions. He wanted such functions to be thought of as operations like addition or subtraction. This is more familiar to those who have studied mathematics.

This is also how the len() function works:

len('Hexlet')  # Calls 'Hexlet'.__len__()

In addition to methods, objects have attributes, but Python's built-in objects don't have many of them. For example, the attribute __doc__, which returns the documentation of the function. Therefore, functions are also considered objects:

len.__doc__ # 'Return the number of items in a container.'

Attributes work and look like variables, only they need to be entered like this: [object].[attribute].

Instructions

Make the string text lowercase and print it on the screen. An example of a method that performs this task is given in the theory.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.