Learn the dynamic type summary for Python language

Source: Internet
Author: User

A variable is simply a reference to a particular type of object at a specific point in time, and when the variable appears in the expression, it is immediately replaced by the currently referenced object, that is, in a program, the same variable name can reference any type of object. Without being limited to its initial reference. The type belongs to the object, not to the variable, assigning a new value to a variable, not replacing the original object, but letting the variable refer to an entirely different object.

>>> a=11

>>> type (a)

<class ' int ' >

>>> a= ' abc '

>>> type (a)

<class ' str ' >


Variables and objects are stored in different parts of memory, connected by connections (Assignment = etc.), and internally, variables are actually a pointer to the object's memory space, which is also referred to as a "reference"

For objects, a chunk of memory is allocated inside the system, with enough space to represent the values they represent, and each object has two standard header information: A type marker, a reference counter, which is used to determine whether the object can be recycled

Garbage Collection of objects

Whenever a variable name aesthetic viewpoint is given a new object, the space occupied by the previous object is recycled (if it is not referenced by another variable name or object), the space of the original object is automatically placed in a pool of free memory space, waiting for the later image to be used

Shared references

Multiple variable names refer to the same object, as in the following example, variables A and B are object 3 created by referencing a constant expression, and when you assign a value to variable a, the value referenced by B does not change.

>>> a=3

>>> B=a

>>> type (b)

<class ' int ' >

>>> a=4

>>> b


Share references and modify in situ

One thing to note is that the list is allowed to be modified in place, so if two variables are referenced together with a list object, then modifying the list by one of the variables will affect the other variable's citation value

>>> L1 = [1,b,3]

>>> l2= L1

>>> l1[1]= ' C '

>>> L2

[1, ' C ', 3]

If you want to avoid this situation, you need to use a copy object, for a list, you can use the Copy module of the standard library, or you can use the Shard from beginning to end, so that two variables point to different memory areas.

>>> l1=[1,2,4]

>>> l2=l1[:]

>>> l1[0]=2

>>> L1

[2, 2, 4]

>>> L2

[1, 2, 4]


For dictionaries and collections, it should be called using x.copy (), which can change objects directly in place, with lists, dictionaries, and some objects defined by class.

>>> Import Copy

>>> l3=copy.copy (L1)

>>> L3

[2, 2, 4]


Shared references and equality

To determine whether the two variables are equal, you can use "= =" and "is", but both have different meanings, "= =" refers to the value of the object referenced by the variable is equal, while "is" refers to the two variables refer to the same object, is just a comparison of the implementation of the reference pointer

>>> l=[1,2,3]

>>> m=[1,2,3]

>>> l==m


>>> L is M



But in order to optimize execution speed, Python has a caching mechanism for immutable objects, such as numbers and string pairs, where objects of the same value generate only one object memory space, that is, when the value of the object referenced by the variable is equal to the values of the immutable objects, such as numbers, strings, tuples, and so on. This object is the same object, you can see the number of times an object is referenced by Getrefcount () in the SYS module

>>> a=5

>>> b=5

>>> A = = b


>>> A is B


>>> str1= "Hello"

>>> str2= "Hello"

>>> str1 = = str2


>>> str1 is str2



>>> Import Sys

>>> sys.getrefcount ("Hello")


>>> str3= "Hello"

>>> sys.getrefcount ("Hello")



Learn the dynamic type summary for Python language

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.