In Python everything is an object

In Python everything is an object


And to be more exact an immutable or mutable one.

Introduction

As the title says everything in Python is an object that said what I am trying to tell you is that everything, from a variable to a function is considered an object.

“ Variables hold an instance of an object”

There are two types of objects, the Mutable and Immutable. Objects in Python the moment they are initialized, are assigned an object id, a type and a value.

Object id.

An object id is “an integer, which is guaranteed to be unique and constant for this object during its lifetime.” in other words this integer is attached to a space in memory or as we call it an address. If you are curious and want to know what is the id of the objets you are working with the id() function will help you ease that curiosity.

Using id() Function
id or address of integers assigned above

Interesting how the integer 10 has the same id even when it was assigned to a different variable.

Object type.

The object’s type tells us what characteristics and operations can be performed to that object. In other words it tells us if we are working with a tuple, list, string, integers, floats … It is important to know the type of the object you are working with as it is the type who determines the mutability of the object. To find the type of the object we use the type() function.

Using the type() function on several objects

Mutable and immutable objects

As addressed before, all objects in Python are of two kinds mutable or immutable. What this means is that mutable objects for example “can change their content without changing their identity.” while immutable objects can’t be changed without affecting their identity.

To explain this topic better we are going to see an example with a string which is an immutable object:

Trying to change the "h" for a "p"
Error produced

Instead of changing the letter h for a p, as we thought it would happen, we got an error, this is because you can’t update a string due to its immutable condition.

On the other hand if we do an example with a mutable object such as a list this will happen:

A list is declared
Value in index 0 is changed from 10 to 40

A list being a mutable object allows it to be changed as in the example above, the number 10 located at index [0] was changed by the number 40. The printed answer is not a new list, it is the same list but with the changes made.

Passing arguments to functions

The importance of knowing about mutable and immutable objects is that at the moment you write functions and pass an argument to it, depending on the type of object you might be managing better memory with one or the other.

When using a mutable object you might want to maintain the original values for later so it would be wise to make a copy. With immutable objects on the other hand, can be called by reference as their value will not be changed.

References:

Python id() - JournalDev Python id() function returns the "identity" of the object. The identity of an object is an integer, which is guaranteed… www.journaldev.com

https://www.oreilly.com/library/view/learning-python/1565924649/ch04s04.html

https://towardsdatascience.com/https-towardsdatascience-com-python-basics-mutable-vs-immutable-objects-829a0cb1530a

要查看或添加评论,请登录

Javier Gutiérrez的更多文章

社区洞察

其他会员也浏览了