Introduction to objects in Python

Ric Hincapie
Level Up Coding
Published in
4 min readMay 27, 2020

--

In Python everything is an object, and each object has one or multiple references. By object, I refer to a collection of data and methods that is physically stored in memory under one single direction. Every object corresponds to an instance of a class, which is like the mold from where it comes and determines its characteristics.

In this blog post, I will do my best to introduce you to the basic concepts around Python objects and how to work with them. Welcome!

Id and type

All the Python objects have an individual, constant, and unique identification number which will not be assigned to another object during its lifetime. It is called identity. The type is a constant classification of the object according to either a built-in “mold” or class, or to users defined ones. The type indicates to the user or programmer specifically what kind of data and methods (operations) can be stored or executed by that object. In a simple way, Id is the object fingerprint and type is the DNA. The third part of the object is its value, which can or cannot change, and refers to all the variables it contains.

So, identity, type and value are the three basic categories that define the Python data model.

Let’s explore with the help of the Id()and type()built-ins functions the details of these concepts:

Example of Id and Type in Python objects

Mutable objects

Mutable objects are those whose value can change. Sequences like lists, dictionaries and sets, and all users defined classes and its instances are in this category.

Again, Id()function will be very handy to inspect the changes make in the next example:

Mutable objects can change while keeping its Id number in Python

Immutable objects

Immutable objects are those that cannot be changed, so once they have their memory position it wont accept any modification of the information it contains. Numbers like integers and floats, sequences as tuples and strings compose this category.

Immutable objects as strings can only change by changing their memory location or Id in Python

Why does this matter to a programmer?

Programming is all about using data and applying methods to it so you get a desired result. This means data is the main resource we work with. The way in which a specific programming language defines, stores and modifies data is a critical information needed to make good and reliable software.

Python optimizes its memory usage by allowing a single object to be referenced in various ways automatically. This is, indeed, what happens with immutable objects but not with mutable ones.

Passing arguments (objects) to functions in Python

There are two different strategies the different programming languages use to pass information from the context to a function: call by value and call by reference. In call by value, the system makes a copy of the context data and passes it on the new function’s context to work with it. This method secures the integrity of the original data because from inside the function it cannot be modified. The disadvantage is it has to use more memory to store the data’s copy. In call by reference, the function receives an implicit reference to the data, allowing it to be modified from within the function and making it susceptible to accidentally being changed.

Python uses the call by object mechanism, which works depending on the kind of value being passed, if it is a mutable or immutable kind. I like to think about it as a hybrid strategy. If the argument is an immutable value, the passing acts similar to a call by value, making a copy of it to be passed. On the other hand, a passing act call by reference will be used if it is a mutable value.

Hope this introductions helps many as a good starting point in the amazing world of Python programming!

--

--