This variable is not a variable (python variable)

Source: Internet
Author: User

everis very curious,pythonthe variableHow to be so flexible that you don't have to declare their typeand can be a variety of casually assigned value, later in-depth study feel really interesting. Pythonin Digitalstrings, etc.Are objects,1is an object,"Hello"is also an object, each object knows its own type, each object has a reference counter, what is the reference counter to do, add the object"Hello"assigning values to variablesa, namely:a= "Hello"thisais the object "Hello "one of the references, and then putaassign a value tob, i.e.B=a, thenaand thebpoint to the same object"Hello"

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/89/wKioL1cDyHmhb_AZAAAW6Z36UU0583.png "title=" Qq20160405221332.png "alt=" Wkiol1cdyhmhb_azaaaw6z36uu0583.png "/>


at this point , the value of the reference counter of Hello is 2 ,python has a garbage collection mechanism, when the reference counter is 0 , the memory occupied by the object is automatically freed. Of course not all objects are like this,Python has a caching mechanism,python caches and re-uses small integers and small strings, such as

>>>a=24

>>>a= "Hello World"

when we assign a new value to a, the object is not recycled as we say, it will probably still be saved in a system table for the next reference, and one more example:

>>>a=45

>>>b=45

>>>a is b

True

at this point , A and B point to the same object .

>>>a=[' A ', ' B ']

>>>b=[' A ', ' B ']

>>>a is b

False

at this point, a,b points to a different object

We can use the is operator to verify thatthe is operator is used to check the identity of the object, and returns True if the two variable names point exactly to the same object. B

we can also query the number of references to Python

>>>import SYS

>>>sys.getrefcount (1)

828

output 828 references, most of which are used by the idle system code.


This variable is not a variable (python variable)

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.