Python3: Mutable, Immutable... everything is object!
https://images.app.goo.gl/zgUTLf952TxVB1MZ9

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

Python is an object oriented programming language, objects are an encapsulation of variables and functions into a single entity, variables are called properties and functions are called methods.[1][2]

Id and Type

Both functions are useful to understand this topic, so lets see how they work.

  • id() is an inbuilt function in Python, that return the identity of an object.

This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes may have the same id() value, I will explain this later.[3]

id(object)


type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes.[4]

Examples of types are integers, tuples, lists...

type(object)

Mutable and inmutable objects

No hay texto alternativo para esta imagen

There are two types of objects in python, mutable and inmutable. 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.[5]

var1 = "hi"
var2 = "hi"

When we execute this in python, we know that both variables will refer to the "hi" string, but we don't know that they are pointing to the same string.

So we have two possible states, first the equality, that is tested using == when the variables have the same value:

>>> var1 == var2
True

And the relationship, when the variables refer to the same object:

>>> var1 is var2
True

Strings are inmutable so python optimizes resources by making two names that refer to the same string value refer to the same object.

Lists are mutables, so:

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True
>>> a is b
False

[6]

Aliasing

If we assign one variable to another, both variables refer to the same object:

>>> list = [1, 2, 3]
>>> alias = list
>>> list is alias
True

Changes made with one alias affect the other:

>>> alias[0] = 5
>>> print list
[5, 2, 3]

[6]

How arguments are passed to functions

When a mutable object is called by reference in a function, the original value ca be changed.

def listadd(list):
    list += [25]

l = [15, 20]
print(id(l))                      #18597456317777

listadd(l)
print(l)                          # [15, 20, 25]
print(id(l))                      #18597456317777

If you don't want to change the original, you have to create a copy inside the function.

In the other hand, inmutable objects can't change because they are passed by value, that means that only the value called no the object itself.

def addNum(x):
    x += 2

n = 5

print(id(n))                      #18597456317777

addNum(n)
print(n)                          # 5
print(id(l))                      #18597456317777


References

[1] w3schools

[2] learnpython

[3] geeksforgeeks

[4] geeksforgeeks

[5] geeksforgeeks

[6] openbookproject

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

社区洞察