Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

4.1 Everything is an Object

So far, and analogous to many other programming languages, you might think of data types like strings or integers as primitive data types, which means that they are built into the core of the language as plain values that behave in special ways and cannot be extended or modified. And this is true in many other programming languages like C++, where an integer is just a raw number stored in memory and not an object.

In Python this is a bit different and there aren’t really any primitive data types, as it is a deeply object-oriented programming language. In Python, everything is an object. Strings are objects, integers are objects, lists are objects, dictionaries are objects, and so forth. We will come back to that concept in detail later in this chapter, but for now let us just focus on some practical implications this will bring.

The dot notation

Let’s start with the dot (.) notation. As you probably already noticed, we previously worked with our variables in two different ways. First, there is a functional syntax, where we pass an object (e.g. a list) as an argument to a function:

4

A function is a specific, reusable piece of code that performs a specific action. In this case, the len() function calculates the length of the list given to it as an argument. It then returns some kind of result, which we can print or save as a new variable. Second, there is the object-oriented syntax which you previously saw when working with strings:

'hello world'

If you have previously only worked with data-centric languages like MATLAB you might find this a bit confusing. But don’t worry, it essentially means that we simply call a function attached to the object (in this case the string). Such object specific functions are called methods, so if you use text.lower() it will simply call the .lower() method of the string object. The dot operator expresses a relationship of belonging, so you can intuitively read text.lower() as “on the text variable, use the .lower() method which belongs to it”.

Inspecting objects

One implication of everything being an object in Python is that we might need to find out what kind of data an object contains and which methods it implements, as this might not always be obvious from just looking at the variable itself. We will not go into too much detail, but briefly introduce some simple ways for you to interrogate objects.

First, you can always see the type of an object by using the built-in type() function:

list

Second, the dir() function will show you all attributes and methods of an object. Many of them start and end with two underscores; these are so-called special (or dunder) methods and attributes which Python mostly uses internally. The regular methods you will typically use are listed at the end.

['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Here you can see that the list object implements 11 different methods starting with append. All of these attributes and methods are available to you to access through the dot notation (e.g. my_list.__class__ or my_list.append()).

list
[1, 2, 3, 4]