Did You Hear? Everything’s An Object!

Referring to Python of course ;)

Michelle Giraldo
2 min readJan 16, 2020
Photo by Owen Beard on Unsplash

In Python… everything is an object! Which makes sense as Python is an object-oriented programming language. If you don’t know what an object is, it’s basically a collection or an encapsulation of data, like variables, and methods, which are functions.

If you make an object, you may need to identify it and there’s multiple ways to do so. Lets say you want to know what kind of object a certain variable was. In order to figure out what type of object this is, you can use the type() function. This is not to be confused with the id() builtin function, which can also help identify an object. But instead of returning the type, the identity is returned. This identity is a long number that is unique and sticks with with the object for it’s entire lifespan.

>>> a = [3] | | >>> type(a) | | <class ‘list’> | |  >>>id(a) | | 140362582921928
Using type() vs id() on the same object

In Python, objects are either mutable or immutable. Mutable objects store a collection of data that can be changed after being created. These objects include lists, sets, and dictionaries. Continuing the example from the photo above, a is a variable for a list. As you can see this list can hold additional data and remain the same object, identified by it’s identity number. After resetting a:

Initializing a list object and adding to it, showing it remains the same object

On the other hand, an immutable object like integers, floats, strings, or tuples, have values that can’t be changed once created. If the variable is changed, a new object is created instead. Like if b was an integer that then changed, it would have a new identity like so:

b was set to an int and when that int changed… the object was changed too

--

--

Michelle Giraldo
Michelle Giraldo

Written by Michelle Giraldo

Graduate of Holberton School, New Haven as the former Student Tutor of Cohort 11, with a completion of the AR/VR specialization.

No responses yet