Python dynamic type

Source: Internet
Author: User

In Python, the type is automatically determined during the run, not through the declaration of the Code. ---is very important.

>>> a=666

    1. Create an object to represent the value 666 (the concept of a type is present in the object rather than in the variable name)

    2. Create a variable A (create a variable when assigning)

    3. Connecting a variable to a new object 3

Variable name and object, after running a=666, variable a becomes a reference to object 666, internally, the variable is actually a pointer to the object's memory space (created by running the constant expression 666).

The connection from a variable to an object in Python is called a reference. A reference is a relationship that is implemented in an in-memory pointer form.

A variable is an element of a system table that has a connection to an object

object is a piece of memory allocated, with enough space to store the value they represent

A reference is a pointer to a variable to an object


An object has two standard header information, a type marker to identify the object's type, and a reference counter to determine whether the object can be recycled.


Garbage collection:

If the object does not have a variable name reference, the Python garbage collection mechanism reclaims the memory. The reference counter of this object is used to determine if there is a reference.


Shared references:

Multiple variable names refer to the same object, called a shared reference ;

>>> a=666

>>> B=a

>>> A

666

>>> b

666


>>> a=666

>>> B=a

>>> b

666

>>> a= ' Fuck '

>>> b

666

>>> when the fuck is finished, a new object (memory space) is created by the constant expression fuck, but the variable B still references the original object 666, which changes the reference to variable a instead of the object 3. Thus variable B has not changed. ( not replace the original object )

Note: Assigning a new value to a variable, instead of replacing the original object, allows the variable to refer to an entirely different object.


Share references and modify in place, the following is the list

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

>>> l2=L1

>>> L2

[1, 2, 3, 4]

>>> L1[0]=666

>>> L1

[666, 2, 3, 4]

>>> L2

[666, 2, 3, 4]

>>>

Shared references and equality

>>> a=1111

>>> b=1111

>>> A is B

False

>>> a==b

True

>>> x=999

>>> y=x

>>> x is y

True

>>> x==y

True

where "= =" compares the values of the two referenced objects, the IS operator checks the identity of the object, and two variables point to the same object.


Note: examples

>>> x=11

>>> x= ' Fuck '

Has object 11 been reclaimed by Python garbage collection immediately? Not at all. Python caches and takes small integers and small strings. Such as:

>>> m=1111

>>> n=1111

>>> N is M

False


>>> c=11

>>> d=11

>>> C is D

True #小的整数和字符串被缓存, multiplexing


>>> g=1000

>>> f=1000

>>> G is f

False


How many times the query object is referenced, using the SYS module's Getrefcount (object)

>>> Import Sys

>>> Sys.getrefcount (11)

20

>>> sys.getrefcount (' Fuck ')

4


Python dynamic type

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.