Python's knowledge of how to allocate memory for different objects

Source: Internet
Author: User

The return value of the ID method is the memory address of the object.

Python allocates memory for each object that appears, even if their values are exactly equal (note that equality is not the same). If you execute a=2.0,b=2.0 these two statements will allocate memory for 2.0 of this type of float object, and then point A and B respectively to the two objects. So A and B are not pointing to the same object:
>>> a=2.0
>>> b=2.0
>>> A is B
False
>>> a==b
True
But in order to improve the efficiency of memory for some simple objects, such as some of the smaller int objects, Python takes the method of reusing object memory , such as pointing to a=2,b=2, because 2 is a simple int type and the value is small, Python does not allocate memory two times. Instead, assign a and B to the assigned object at the same time:
>>> a=2
>>> b=2
>>> A is B
True
If you do not assign a value of 2 instead of a large value, the situation is the same as before:

>>> a=5555
>>> b=5555
>>> A is B
False
>>> ID (a)
12464372
>>> ID (b)
12464396

Reference:

1, https://www.cnblogs.com/dplearning/p/5998112.html

Python's knowledge of how to allocate memory for different objects

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.