In Python... everything is object!
https://miro.medium.com/max/6000/1*X4PYcARyk83gOThpyR_iWg.jpeg

In Python... everything is object!

Object-oriented programming (OOP) is supported by most programming languages, providing advantages like clear structures for reusable programs with less code. This programming paradigm is based on the concept of "objects", which can contain data and code. With this approach, programs are designed by making them out of objects that interact with one another. Python, being a high level programming language, basically revolves around OOP because it manages everything as an object.

id and type

The built-in method id() is a function that returns the identity of an object, a memory address presented as an integer, which is unique for the duration of the object's life. In Python, every object is represented by a general structure, which can be used to describe and access an object’s information and methods, respectively. This information is an object’s type. The built-in method type() is a function that returns the type of an object.

>>> a = 1

>>> id(a)

10105088

>>> type(a)

<class 'int'>

In the previous interactive session we can see an example of both tools in action. They take the variable a as parameter, which refers to the object 1, thus providing valuable information from the object that can be useful when programming.

Mutable and Immutable objects

Objects are split into two kinds: mutable and immutable. Python handles them differently. Mutable objects are changeable, which means that you can change their content without changing their identity. This kind of object includes: sets, dictionaries, lists, custom classes and class instances, among others. Mutability is beneficial to use when you need to change the size of the object. On the other hand, immutable objects are not changeable and their state can't be modified after they are created. This kind of object includes: tuples, strings, numbers, among others. Immutability ensures that an object remains constant throughout the program. Lets go over different examples in order to better understand how Python treats both kinds of objects.

>>> a = "hello"

>>> b = "hello"

>>> a == b

True

>>> a is b

True

Above, we have the string(immutable) "hello", which can be referred by the variable names a or b. With the operator == we can test if two names have the same value. While with the operator is we can test if the two names refer to the same object.

>>> a = [1, 2, 3]

>>> b = [1, 2, 3]

>>> a == b

True

>>> a is b

False

Now we are testing the same but with a list(mutable). Note that even thought the values are the same, the names refer to a different list. The identity of a mutable object is not tied to its value. So, when creating an instance of a mutable object such as a list, Python won't look for existing instances of the same value, instead it will allocate a new instance of the object. This is why is very important to visualize the variable names as a resource to get to an object and not as the object itself. Meanwhile, if we assign one variable to another, both variables refer to the same object.

>>> a = [1, 2, 3]

>>> b = a

>>> a is b


True

>>> b[0] = 5

>>> print(a)

[5, 2, 3]

As we can see, the same list is referred by both a and b, which means it is aliased. An alias is simply an alternative name for the same object. If we were to make changes to a list and keep the original a cloning takes place, thus a new object being created. Lets check the difference in the following example.

>>> a = [1, 2, 3]

>>> b = a[:]

>>> print(b)

[1, 2, 3]

>>> b[0] = 5

>>> print a

[1, 2, 3]

>>> id(a)

10102780

>>> id(b)

10102679

Here we used the slice operator [:] with a. Slicing creates a new list, in this case the slice was of the whole list. Another way of creating a new list is by using the method list().

You can change a single value of a mutable data type and it won’t change its memory address. However, if you try to change a single value of an immutable type it will throw an error. Nevertheless...

>>> robot = (2021, ["grey", "two arms"])

>>> attributes = robot[1]

>>> print(attributes)

["grey", "two arms"])

>>> attributes[0] = "red"

>>> print(robot)

(2021, ['red', 'two arms'])

In this example we can observe how a tuple(immutable) changed. That is why understanding the aliasing process is essential. What is immutable is the physical content of a tuple, consisting of the object references only. The value of the list referenced by robot[1] changed, but the referenced object id is still the same. A tuple has no way of preventing changes to the values of its items, which are independent objects and may be reached through references outside of the tuple. Lists and other mutable objects inside tuples may change, but their ids will always be the same.

How arguments are passed to functions?

If we were to pass an integer to a function, that integer will keep its identity beyond the scope of the function, because integers are immutable. The reference to the integer passed to the function becomes a new object within the scope of the function. Otherwise, if we pass a list to a function any changes will be registered beyond the scope of the function. This behavior occurs because list objects are mutable, so they can withstand changes without becoming a new object. When programming in Python it is important to note the mutability of the objects that the variables are referring to for a proper implementation of a program.

References:

https://en.wikipedia.org/wiki/Object-oriented_programming

https://www.w3schools.com/python/ref_func_list.asp

http://www.openbookproject.net/thinkcs/python/english2e/ch09.html#objects-and-values

http://radar.oreilly.com/2014/10/python-tuples-immutable-but-potentially-changing.html

http://composingprograms.com/pages/24-mutable-data.html#sequence-objects






To view or add a comment, sign in

More articles by Jeffrey Martinez

Explore content categories