Where object initialization in Python is placed in memory

Source: Internet
Author: User

Screenshot from the "Python Source code Anatomy" The first page of the body.

Also namely:

When used in another class

, node is in the heap. is equivalent to calling malloc in C.

More than just a class object such as node is present in the heap, the value of the int type in Python is also present in the heap. Because even the basic data type is the same object in Python.

Let's consider the two variables of value and Self.value in node:

Why can other methods in node call Self.value without being able to call value?

I used to understand this problem in the C + + thinking method, and now it seems to be wrong: previously thought that value is a dynamic variable in a function (method), placed in the stack memory, when the function (method) will automatically release the value of the dynamic variable after the end. According to the above understanding, value is also placed in the heap, if understood as value after the end of the function will be the Python core auto-recovery can be explained.

And it's easy to understand why Self.value works: the self points to the heap memory that node initialization is, and in this memory (which belongs to the initialized instance) there is value in the space to hold the value of this instance, so it can be accessed outside of the function.

Novice understanding, wrong Please correct me, thank you very much!


Where object initialization in Python is placed in memory

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.