Everything is object in python

Julian Torres
4 min readSep 30, 2020

Anyone who wants to learn how to program Python should know that all he finds is an object and that there are mutable and immutable objects, see the following table:

Now, let’s dig a little deeper, everything in python is an object and each object has its unique object id, with this we come to the conclusion that a mutable object can be changed after its creation and an immutable not, to know if our object is mutable or we will not see the functions ID and TYPE

ID and TYPE

The id() function returns the identity of an object. This ensures that it is the only one between simultaneously existing objects (is the memory address of the object).

The type() function returns the type of the object it receives as an argument.

Mutable objects

Mutable objects can change their contents and immutable objects may not

The mutable objects are: lists, dictionaries, sets, arrays

Immutable objects: int, float, complex, string, tuple, frozen set [note: immutable version of set], bytes

When a variable is of an immutable type, such as a string, it is possible to assign a new value to that variable, but it is not possible to modify its contents.

>>> a"example"
>>> a"other"
>>> a[2]"c"
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
TypeError: 'str' object does not support item assignment

This is because when a new mapping is made, the string itself is not modified, but the variable is passed to point to another string. However, it is not possible to assign a new character in a position, as this would involve modifying the immutable string.

For mutable parameters, the mapping has the same behavior, that is, the variables point to a new value.

>>> list1 =[10, 20, 30]
>>> list2 =list1
>>> list1 =[3, 5, 7]
>>> list1
[3, 5, 7]
>>> list2
[10, 20, 30]

Function arguments passing in Python

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

Python immutable function arguments

Python immutable objects, such as numbers, tuples, and strings, are also passed by reference as mutable objects, such as list, set, and dict. Because of the state of immutable objects if an integer or string value is changed within the function block, it behaves a lot like a copy of objects. A new local duplicate copy of the caller object is created and manipulated within the scope of the function block. The caller object will remain unchanged. Therefore, the call block will not notice any changes made within the scope of the function block on the immutable object.

>>> def list_add3(lin):
lin += [3]
return lin

>>> a = [1, 2, 3]
>>> b = list_add3(a)
>>> b
[1, 2, 3, 3]
>>> a
[1, 2, 3, 3]

Here, the error is to think that lin, as a parameter of the function, can be modified locally. Instead, lin and reference the same object. Because this object is mutable, the modification is done in-place, which means that the object referenced by both lin and a is modified. We do not need to return lin , because we already have a reference to this object in the form of a . a and b end up referencing the same object.

Python Mutable function arguments

Python mutable objects such as dict and list are also passed by reference. If the value of a mutable object is changed within the scope of the function block, its value is also changed within the scope of the caller or parent block, regardless of the argument name.

>>> def tuple_add3(tin):
tin += (3,)
return tin

>>> a = (1, 2, 3)
>>> b = tuple_add3(a)
>>> b
(1, 2, 3, 3)
>>> a
(1, 2, 3)

At the beginning of the function, tin and reference the same object. But this is an immutable object. So when the function tries to modify it, it tin receives a new object with the modification, while a maintains a reference to the original object. In this case, returning tin is required, or the new object would be lost.

--

--