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