Does Python have the concept of heaps and stacks?

Source: Internet
Author: User
a = "ABC" b = a a = "XYZ" Print (b) Print (a)

Reply content:

The type of stack and heap that the JVM and the CLR should be separated from should be no, as Python should have only the heap, but there is a constant pool. Chen Ru teacher and Rai Yonghao Teacher's "Python Source code Anatomy" has detailed introduction this aspect knowledge.
================
For many years do not read the source analysis, my statement is problematic, comments in Rednaxelafx has fixed: "CPython not only have stacks and stacks there are native stack and pyframeobject composition of the interpreter stack on both sides." Disclaimer: The following Python implementations or related detail concepts are referred to as CPython

1. First solve the question in the main demo code inside the puzzle
(not written compiler) whether it's Python or C, a very important concept is name and object. In Python, the assignment statement has a very concise abstract concept, as follows
Assignment expression in Python has a things in common:
        • Create a new object
        • Build a connection between that new object and a name
The Lord can think that since a = 1 is assignment expression in Python, then def func (): Pass is not an assignment statement? (Please fasten the above two concepts on the assignment expression) I did not say a digression, but in the help of the master more profound understanding of the demo code in the value of the assignment over the past some doubts.

On the one hand, in order to make better use of high-efficiency memory, on the other hand, in order to release the programmer from a variety of memory management details of liberation, better focus on "business logic", with GC and some very cool technology, for example reference

inch [8]: string_1 = "EOF"inch [9]: string_2 = "EOF"inch [ One]: Print "Address 1:%x" % ID(string_1)Address 1: 7f2517898530inch [ A]: Print "Address 2:%x" % ID(string_2)Address 2: 7f2517898530
Please do not use the Java set of ideas to understand Python. The compiler does not understand. Do not comment.
But the string is a constant in Python, and the implementation is probably something like a constant pool.
The value of STR cannot be changed, but only the address that the variable tag points to.
a = "str"b = "str"a is b
Remember one thing: CPython, for example, all variables are a pyobject*, that is, pointers, and all assignment statements are assigned to the address of an object to it, in fact, not only the variables, but also inside the container, such as list and dict elements, so your code here is equivalent to:
Pyobject *a = NEW_STR ("abc");
Pyobject *b = A;
A = New_str ("xyz");
Of course, the inside is not with new_str this function, the specific name I forgot

Python reference issues I used to share in a group, someone organized a chat record:
Python object reference-python2 group 20130223 in-group instruction record
Can look at, welcome to discuss see this question, throat, want to say what, do not know to say what ....
1. A and B are two different variables, a = b is a variable assignment ah, not a reference assignment. Of course, the printing results are not the same. This is a question of Python syntax.
2. Heap and Stack, when the heap and stack two put together to say, of course, refers to the C language in the special heap and special stack, rather than the data structure of the stack. So some answers are wrong.
3. Python language and Python virtual machine problem, Python virtual machine is just an implementation of the Python language, if the virtual machine is written in C, it is of course there are heaps and stacks, the question is, is there a manual to define the specific implementation of the method?
4. If the Python language itself, the question that you see in the Python book that talks about heaps and stacks, I mean, planing the language implementation part.
So, this question ... Juvenile, please understand the basic concepts to discuss it again. A and B should be just a label.
  • 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.