Python: Everything is an object!

Christian Martinez
5 min readOct 1, 2021

Hi peer programmers! I’ll write about python objects and their characteristics;

  • Python Semantics; what’s and object with attributes and methods
  • Class vs Objects vs Instances
  • Object ID, type, and value
  • Mutable vs immutable objects
  • Alias, aliasing, reference, and asignment

Let’s buckle up and get to the fun already!

.

To start with, let’s talk about Python Object-Oriented programming knowledge.

So… ¿Everything is an object?

Indeed.

Python is an object-oriented programming language; everything in Python is an object, and almost everything has attributes and methods.

“An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. It’s not an idea anymore, it’s an actual dog, like a dog of breed pug who’s seven years old. You can have many dogs to create many different instances, but without the class as a guide, you would be lost, not knowing what information is required.” GeeksForGeeks, Python Classes and Objects.

All data types; integers, lists, dictionaries, strings, and tuples are objects in Python. They all have specific methods and are capable of storing data of different characteristics. They’re all basically different sized objects created under a blueprint (Class).

.

Let’s clarify some new terms before moving forward.

Class vs Objects vs Instances

Sheep Class, Objects, and Instances.

¿What are the differences between Classes, Objects and Instances?

A class is a blueprint which you use to create objects. It has Attributes and methods (actions) shared by its instances.

An object is an instance of a class — it’s a concrete ‘thing’ that you made using a specific class. Besides inherit the Class Attributes and Methods, they may have their own instance attributes.

The word ‘instance’ indicates the relationship of an object to its class.

Let’s use the Sheep above for a more graphic explanation;

  • The Class “Sheep” is the blueprint for creating objects. It has attributes (name, penNumber) and methods (Sheep probably sleep and eat ¿Right?)
  • Every Sheep is an object and an instance of the Class Sheep.
  • Every Sheep probably has unique Attributes that differentiate them from other sheep (Instance Attributes).

.

Now, let’s bring up another concept.

Object ID, type, and value

As you can see in the previous code, The content, ID, and type of any data are very different.

The value and data type of variable is pretty much self-explanatory. However, when it comes to ID that’s a different story.

“In Python, every object that is created is given a number that uniquely identifies it. It is guaranteed that no two objects will have the same identifier during any period in which their lifetimes overlap. Once an object’s reference count drops to zero and it is garbage collected, then its identifying number becomes available and may be used again.” Real Python, Object Value vs Object Identity.

That’s very cool indeed! So, we can check the ID of anything, remember; Everything in Python is an object!

Something important to note, is that in case two objects have the same type and value, it will make one of them point to the other and both will share the ID.

In this example I gave var_1 and var_2 the same value, so they will share the same ID.

I declared Var_2 = Var_1 + 1 on this code. Whenever the value or data type of the variables change, Python will assign a new ID and space in memory.

.

Immutable vs mutable objects

Mutable objects can be changed, immutable objects can’t change once they’re created. That’s why the ID of immutable objects will never change once they’re created. Nevertheless, mutable object’s ID changes with the value and data type (in case of changing it)

List, dictionary, set and user-defined classes are mutable. Int, float, decimal, boolean, string, tuple, and range are immutable data types.

.

Alias and Aliasing

An alias is a second name for a piece of data. Aliases are created when it's easier to have a second way to refer to data, than making a copy of it.

Immutable data can be referred as many times as needed; the data won’t change. If data can change (mutable data), aliasing can lead to difficult bugs to find.

Aliasing happens when one variable’s value is assigned to another. Let’s remember that variables are just names that store references (ID’s) to the values inside.

In this example we can see both objects reference the same value in memory. In this case, they’re referencing an immutable object that can’t change.

Let’s look at the following example:

Here, we changed the value of list_1 indirectly, by modifying list_2.

This is called “Side Effect” and leads to bugs.

.

I hope you found this article fun and useful! Keep tuned for more Python and Linux articles. 🧐

.

Bibliography

GeeksforGeeks, Python Classes and Objects
W3schools, Python Classes and Objects
GreatLearning, Understanding Mutable and Immutable in Python

--

--

Christian Martinez

I'm a Software Development student, musician and Athlete.