What does it mean that everything in Python is an object?

Najma Bader
3 min readSep 7, 2021

--

And other common phrases that you will hear in your Python programming life.

Photo by Christophe Hautier

Introduction

If you have been working with Python for a bit, you probably have encounter expressions like “Python is an object-oriented programming language”, “Everything in Python is an object”, “Python is a dynamically typed language” etc.

Practically speaking, you could work on a daily basis using Python without really knowing what these phrases mean and what a Python object truly is. However, if you are curious, here below you will find a long and exhaustive explanation of a Python object is and (in the conclusion) you will find an explanation to the mysterious three sentences we have cited above.

Python objects could be understood at two levels: at a physical level (how they are created in memory) and at a behavioural one (what their characteristics are).

A Python object exists physically in your computer memory

In Python, whenever you define a variable, you are also creating an object (a sequence of bytes) that Python is storing in memory.

For example, when you write my_name = 'Paul', Python is creating, behind the scenes, both a variable name(also called “reference” or “name”) and an object 'Paul' (in this case, it is a string object). The variable my_nameis just a pointer that points to an address in your computer memory where the object is stored.

In summary:

you could think of an object as a sequence of bytes that Python is storing in memory and that you can retrieve using its name.

If you want a go deeper into the topic of memory allocation, you can read this article.

A Python object has some characteristics

In Python, an object must:

  1. be an instance of a class
  2. have some attributes

Almost everything that you use in Python is an object because it's both an instance of some class and because it has some attributes. I say “almost” because this doesn’t hold for keywords (such as for, if, def etc). A list of all Python keywords can be found here.

What does it mean that an object is an instance of a class?

As we have seen, when you define a variable, Python creates a pointer to an object in memory. This object is an instance of a class. You can use the function type() to see the class your variable belongs to.

>>> x = 100
>>> type(x)
int

int is the class itself - not a string representing it. Indeed, you could use this class to create other integers. You could think of classes as "factories" that create new objects of the type that they describe.

# the class int is taking a string as a parameter and creating an integer
>>> int('123')
123

Going one step further, since we said that “everything is an object”, classes must also be objects. But, if every object is an instance of a class, then also the classes must be an instance of some classes.

It turns out that in Python there is a metaclass type of which all classes are instances. You can think of it as a sort of "universal" factory that creates factories (our classes).

>>> type(int)
type

What does it mean that an object has attributes?

Every object in Python is defined with some attributes. To see the default attributes of an object you can use the dir function:

dir(x)

Attributes can be methods or simply store some data. For instance:

import os>>> os.sep  # returns the string (some data) that shows the default path separator of your operating system
'/'

Attributes can also be modified:

os.sep = '\' # change to Window path separator

or new ones can be created:

os.pc_owner = 'hb'

Conclusion

  • Python is an object-oriented programming language: we say so because almost everything in Python is an object.
  • Everything in Python is an object: because almost everything (apart from reserved keywords) is an instance of a certain class and has some attributes.
  • Python is a dynamically typed language: in Python, variables don’t have types but the object they point to do. This means that the same variable can point to different objects over time. In contrast, in static languages, variables can only point to one type.
References
Object-oriented Python Course by Reuven Lerner

--

--

Najma Bader

I grew up in a hippie family but I fell in love with coding. I write about my experience with Python, Big Data and other nice things ❤️