Change skin with Python3: Mutable, Immutable... everything is object!

Change skin with Python3: Mutable, Immutable... everything is object!

Python has a reputation to be easy to learn, even mentioned in the Easter egg, Zen of Python:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated...

but if you go deeper you will find some interesting aspects about this programming language the snake loves the objects almost everything is an object in Python!, but What's an object? Everyone knows what an object is: a tangible thing that we can sense, feel, and manipulate.The meaning of an object in software development is not different. Software objects not be tangible things that you can sense, or feel, but you can model real-world things that can do certain things, an object is a collection of data and associated functions. Take a orange, the orange juice represent procedures to manipulate the data (squeeze the orange), the orange's properties are color and size, the methods of this one would be make orange juice and the event When I drink the juice, if you want know more about Object-oriented programming (OOP) The process of converting a defined object and design a program that does whatever, I share you this awesome tutorial;

Type And Id

Every Object in python has a Id , Type and value

The Type() function returns the type of an object.....easy!, let's look this code

>>> CI = 101
>>> type(CI)
<class 'int'>
>>> print(int is type(CI))
True
>>> print(int not type(CI))
False

well, type returns an 'int' class by transforming our variable into an instance of the int class.

The Id() function returns the identity of an object, its address in memory. This is an integer that assures to be unique and constant for this object during its useful life.

>>> id(CI)
9065824

the id of the CI variable is 9065824, all depends of your machine this value can changes

Mutable Objects

Mutable object: Object that can be changed after creating it;

Lists, Dictionary and Sets

No alt text provided for this image
  1. We created a list of int
  2. We assign a to b
  3. We then appended an element to a
  4. And when we print b it's also update

that is because a and b points to the same object but whats happen if we assign a copy The clone works and b is now point to a different object than a, as their ids are different, you can also create a copy with the method slicing, let’s devise a little experiment

No alt text provided for this image

What's happen here?

  1. We created a matrix (a list inside a list)
  2. and assign a to b
  3. We compare the id of both variable and points the same object
  4. We created a clone with the slicing method
  5. We check b points to another object
  6. we modify the variable a and print variable a and b

b and a are different but when you modify one of the child objects in variable a, this modification will be reflected in variable b as well — that’s because both lists share the same child objects and vice-versa

No alt text provided for this image


Immutable object: Object that cannot be changed after creating it.

Int, Float, Tuple, Bool and Strings

No alt text provided for this image

tuples, namedtuples are immutable. When you try to overwrite one of their fields, you’ll get an AttributeError exception:

How Python treats mutable and immutable objects differently

Immutable objects are quicker to access and are difficult to change because it involves the creation of a copy.While mutable objects are easy to change.Use of mutable objects is recommended when there is a need to change the size or content of the object.

However, there is an exception in immutability as well. We know that tuple in python is immutable. But the tuple consists of a sequence of names with unchangeable bindings to objects.

Consider a tuple

How arguments are passed to functions and what does that imply for mutable and immutable objects

Instead of storing a copy of the parameter in the variable, Python simply has the variable refer to the value. Remember that everything is an object in Python; this means the variable now points to the object, the same object that is the parameter value

if an integer or string value is changed inside the function then it much behaves like an object copying.  new duplicate copy of the caller object inside the function is created and manipulated. The caller object will remain unchanged.

No alt text provided for this image

If the value of a mutable object is changed inside the function then its value is also changed inside the caller or main function

No alt text provided for this image


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

社区洞察

其他会员也浏览了