Python3: Mutable, Immutable... everything is object!
Python - Mutable, Immutable... everything is object!!!

Python3: Mutable, Immutable... everything is object!

Introduction

All things in Python are objects: we have to repeat this constantly whenever we start a discussion about Python because this is the core of the language. So, it is important to know that every object can be either mutable or immutable.


An object that is mutable is likely to change its state. Conversely, an immutable object is immutable in time, and therefore cannot be changed. When we assign an object to an identifier, the identifier contains an instance of the object. This gives the object a unique identifier that can be accessed by calling the identifier as a parameter in id(). At runtime, the object type will be set and will not change. The only thing that can be changed about an object is its state if it is a mutable object.

______________________________________________________________

ID and TYPE

The built-in function id() returns the identity of an object as an integer. This integer usually corresponds to the object’s location in memory, although this is specific to the Python implementation and the platform being used. The is operator compares the identity of two objects.

The built-in function type() returns the type of an object. Lets look at a simple example id():

>>> a = 10
>>> print("id of a",id(a))
id of a 1579869088
>>> b = "Hello World"
>>> print("id of b",id(b))
id of b 53798096
>>>         

The type() function returns the type of the specified object:

>>> a = 10
>>> print("type of a is",type(a))
type of a is <class 'int'>
>>> b = 1.2
>>> print("type of b is",type(b))
type of b is <class 'float'>
>>> c = True
>>> print("type of c is",type(c))
type of c is <class 'bool'>
>>> d = [1,2,3]
>>> print("type of d is",type(d))
type of d is <class 'list'>
>>> e = (1,2,3)
>>> print("type of e is",type(e))
type of e is <class 'tuple'>        

________________________________________

Mutable vs Immutable Objects in Python

Every variable in python holds an instance of an object. There are two types of objects in python i.e. Mutable and Immutable objects. Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at the runtime and it can’t be changed afterwards. However, it’s state can be changed if it is a mutable object.

To summarise the difference, mutable objects can change their state or contents and immutable objects can’t change their state or content.

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.

Aucun texte alternatif pour cette image

Exceptions in immutability

Not all of the immutable objects are actually immutable. Confused? Let me explain.

As discussed earlier, Python containers liked tuples are immutable. That means value of a tuple can't be changed after it is created. But the "value" of a tuple is infact a sequence of names with unchangeable bindings to objects. The key thing to note is that the bindings are unchangeable, not the objects they are bound to.

Let us consider a tuple t = (‘holberton’, [1, 2, 3])

The above tuple t contains elements of different data types, the first one is an immutable string and the second one is a mutable list.The tuple itself isn’t mutable . i.e. it doesn’t have any methods for changing its contents. Likewise, the string is immutable because strings don’t have any mutating methods. But the list object does have mutating methods, so it can be changed. This is a subtle point, but nonetheless important: the “value” of an immutable object can’t change, but it’s constituent objects can.

How objects are passed to Functions

Its important for us to know difference between mutable and immutable types and how they are treated when passed onto functions .Memory efficiency is highly affected when the proper objects are used.

For example if a mutable object is called by reference in a function, it can change the original variable itself. Hence to avoid this, the original variable needs to be copied to another variable. Immutable objects can be called by reference because its value cannot be changed anyways.

def updateList(list1):
    list1 += [10]n = [5, 6]
print(id(n))                  # 140312184155336updateList(n)
print(n)                      # [5, 6, 10]
print(id(n))                  # 140312184155336        

Lets take a look at another example:

def updateNumber(n):
    print(id(n))
    n += 10b = 5
print(id(b))                   # 10055680
updateNumber(b)                # 10055680
print(b)                       # 5        

In the above example the same object is passed to the function, but the variables value doesn’t change even though the object is identical. This is called passby value. So what is exactly happening here? When the value is called by the function, only the value of the variable is passed, not the object itself. So the variable referencing the object is not changed, but the object itself is being changed but within the function scope only. Hence the change is not reflected.

for more information I invite you to watch this short video

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

Dan Bethel Iryivuze的更多文章

社区洞察

其他会员也浏览了