This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

All values are instances of objects

Objects

In Python, all values are (in a sense) objects.

So 356, 3.2, 4+5j, "Imperial", False are all objects.

Do you not believe me? Try these:

>>>> isinstance(2020, object) 
>>>> isinstance(4.33, object)
>>>> isinstance(5j, object)
>>>> isinstance(True, object)
>>>> isinstance("COVID-19", object)

You should read these as “2020 is an instance of the type object. True or false?”.

We will discuss objects in more detail in a later module.

For now, all you need to know is that you can create a new object with the name of the type (or class), followed by a paranthesis and some input argument. This is called a constructor.

>>> int()
>>> float()
>>> complex()
>>> int(-9.789)
>>> float(9)
>>> complex(1,2)
>>> type(complex(1,2))

Note that the constructors may perform type casting to convert its input argument to a valid value.

Objects can also have attributes (something the object has), written as object.attribute.

>>> number = complex(3, 4)
>>> number.real
>>> number.imag

You can read this as “number’s real attribute” and “number’s imag(inary) attribute”.

Objects can also have methods (something the object can do), written as object.method().

>>> "london".capitalize()

We will discuss this topic in more depth later. For now, all you need to remember is that all values are objects.