Dynamic types of Python

Source: Internet
Author: User

Python variable declaration: The Python type belongs only to the object, not to the variable

In Python, the variable declaration does not have to specify the type of the variable as in other languages (c, C + +), the type of python is stored in the object, and the variable is simply a reference to the object, equivalent to a pointer that simply saves the address of its object. (Python object contains two header information, one is type marker, one is reference count)

Python variable assignment:

The Python variable assignment does not change the object currently pointed to, but instead changes its reference.

Such as:

1 a =  #  A holds 42 in-memory address 2 b = A   #  A is assigned to B, and the address assignment b,b and a point to the same address  3 a =  #  33 assigns the address in memory to the memory address that A,a points to 33
One of the Python optimizations: Caching and Reusing objects:

Such as:

1n = 2E102A = n + 13b = n + 14 Print(A = = b)#True5 Print(A isb#False6 7n = 438A = n + 19b = n + 1Ten Print(A = = b)#True One Print(A isb#True A  -  -x = 42 they = 42 - Print(x = = y)#True - Print(x isY#True -  +m ='ABC' -n ='ABC' + Print(M = = N)#True A Print(M isN#True

Python caches and re-uses small integers and small strings

Dynamic types of Python

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.