Everything is object !

Jacer Dabbabi
4 min readMay 27, 2020

Introduction :

An object is simply a collection of data (variables) and methods (functions) that act on those data. And, class is a blueprint for the object.

For instance, an object could represent a person with a name property, age, address, etc., with behaviors like walking, talking, breathing, and running. Or an email with properties like recipient list, subject, body, etc., and behaviors like adding attachments and sending.

ID and Type :

Python id() function returns the “identity” of the object. The identity of an object is an integer, which is guaranteed to be unique and constant for this object during its lifetime.

Python cache the id() value of commonly used data types, such as string, integer, tuples etc. So you might find that multiple variables refer to the same object and have same id() value if their values are same.

“Caching can work only with immutable objects, notice that integer, string, tuples are immutable.”

If a single argument (object) is passed to type() built-in, it returns type of the given object. If three arguments (name, bases and dict) are passed, it returns a new type object.

If you need to check type of an object, it is recommended to use Python isinstance() function instead. It’s because isinstance() function also checks if the given object is an instance of the subclass.

When you have a variable that cannot be changed after it has been created it is called immutable, while in the opposite case, that variable is called mutable.

Mutable objects :

Mutable objects are objects that can change. Two types of mutable objects in Python are: list, dict, set, byte array and user defined classes.

We can change their content without changing their identity

Immutable objects :

These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can’t be changed after it is created.

why does it matter and how differently does Python treat mutable and immutable objects :

Python handles mutable and immutable objects differently when modifying them : immutable are quicker to access than mutable objects , mutable objects are great to use when you need to change the size of the object and immutables are used when you need to ensure that the object you made will always stay the same . Plus , immutables require more memory because changing them involves creating a copy but on the other hand changing mutable objects does not.

Exception : However, there is an exception in immutability as well. We know that tuple in python is immutable. But the tuple consists of a sequence of names with unchangeable bindings to objects.
Consider a tuple

tup = ([3, 4, 5], 'Holberton')

The tuple consists of a string and a list. Strings are immutable so we can’t change its value. But the contents of the list can change. The tuple itself isn’t mutable but contain items that are mutable.

how arguments are passed to functions and what does that imply for mutable and immutable objects :

Arguments are always passed to functions by reference in Python. The caller and the function code blocks share the same object or variable. When we change the value of a function argument inside the function code block scope, the value of that variable also changes inside the caller code block scope regardless of the name of the argument or variable. This concept behaves differently for both mutable and immutable arguments in Python.

In Python, integer, float, string and tuple are immutable objects. list, dict and set fall in the mutable object category. This means the value of integer, float, string or tuple is not changed in the calling block if their value is changed inside the function or method block but the value of list, dict or set object is changed. Consider the mutable and immutable as states of the function arguments in Python

Python immutable objects are also passed by reference like mutable objects . Due to state of immutable (unchangeable) objects if an integer or string value is changed inside the function block then it much behaves like an object copying. A local new duplicate copy of the caller object inside the function block scope is created and manipulated. The caller object will remain unchanged. Therefore, caller block will not notice any changes made inside the function block scope to the immutable object.

Python mutable objects are also passed by reference. If the value of a mutable object is changed inside the function block scope then its value is also changed inside the caller or main block scope regardless of the name of the argument.

--

--