Linux mutable types and immutable types

Source: Internet
Author: User

Each Python object is divided into mutable and immutable, the main core type, where numbers, strings, tuples are immutable, and lists and dictionaries are mutable.

Variable types and immutable types some places differ greatly, pay attention to differentiate.

The re-assignment of variables of immutable types is essentially recreating an object of immutable type and re-pointing the original variable to the newly created object (i.e., if no other variable references the original object (that is, the reference count is 0), the original object is recycled).

Immutable types

Take the int type as an example: actually i + = 1 is not really on the original int object +1, but instead re-create an int object with value 6, I refer to this new object.

You can see that the memory address will change when i + = 1 o'clock is executed because the int type is immutable.

Change the code, but the variable values of multiple int types are identical, to see if they have the same memory address.

For immutable type int, regardless of how many immutable types are created, as long as the values are the same, point to the same memory address. In the same situation there are short strings, floating-point types.

Variable type

Take a list of lists as an example

List after append, still point to the same memory address, because list is a mutable type, can be modified in place.

To change the code, when there are multiple variable type variables with the same values, see if they point to the same memory address as the immutable type.

As you can see from the running results, the values of A and B are the same, but the memory addresses that point to are different. We can also make them point to the same memory address by assigning a value of B = A:

This time to note, because A, B points to the same memory address, and a, B types are list, mutable type, a, b any list modification, will affect the value of another list.

mutable elements and immutable elements of elements in a class

Linux mutable types and immutable types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.