Variable types and immutable types of Python

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.

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).

The immutable type takes an int type as an example: actually i + = 1 is not really on the original int object +1, but instead re-creates an int object with value 6, I refers to this new object.

>>> i = 5>>> i + = 1>>> I6

Verify the memory address of the variable i through the ID function (using hex (ID (i)) to see the 16-binary memory address)

>>> i = 5>>> i + = 1>>> i6>>> ID (i) 140243713967984>>> i + = 1>>> i7> ;>> ID (i) 140243713967960

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.

>>> i = 5>>> j = 5>>> ID (i) 140656970352216>>> ID (j) 140656970352216>>> k = 5& Gt;>> ID (k) 140656970352216>>> x = 6>>> ID (x) 140656970352192>>> y = 6>>> id (y) 140656970352192>>> z = 6>>> ID (z) 140656970352192

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. There are also short strings in the same situation.

For other types, in the case of floating-point types, you can see from the result of the code that it is an immutable type: After modifying the value of I, point to the new memory address.

>>> i = 1.5>>> ID (i) 140675668569024>>> i = i + 1.7>>> i3.2>>> ID (i) 140675668 568976

Modify the code to declare two floating-point variables of the same value, view their IDs, and find that they do not point to the same memory address, which is different from the int type (this involves the Python memory management mechanism, Python has cached the int type and the shorter string, regardless of the number of variables that declare the same value, are actually pointing to the same memory address. )。

>>> i = 2.5>>> ID (i) 140564351733040>>> j = 2.5>>> ID (j) 140564351733016

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

>>> a = [1, 2, 3]>>> ID (a) 4385327224>>> a.append (4) >>> ID (a) 4385327224

Change the code, when there are multiple immutable type variables with the same values, see if they point to the same memory address as a mutable type

>>> a = [1, 2, 3]>>> ID (a) 4435060856>>> B = [1, 2, 3]>>> ID (b) 4435102392

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:

>>> a = [1, 2, 3]>>> ID (a) 4435060856>>> B = [1, 2, 3]>>> ID (b) 4435102392>>> b = a>>> ID (b) 4435060856

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.

>>> B.append (4) >>> a[1, 2, 3, 4]>>> b[1, 2, 3, 4]>>> ID (a) 4435060856>>> ID (b ) 4435060856

In the code, the B variable append (4) is also affected by the A variable. Output their memory address, or point to the same memory address.

Variable types and immutable types of Python

Related Article

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.