Does python have the concept of heap and stack?

Source: Internet
Author: User
AabcbaaXYZprint (B) print ()
a = "abc"b = aa = "XYZ"print(b)print(a)
Reply: there should be no distinction between stack and heap of JVM and CLR. python should only have heap, but there is a constant pool. The Python source code analysis by Mr Chan and Mr Lai Yonghao gives a detailed introduction to this knowledge.
======================
I have not read the source code analysis for many years. I have a problem with this statement. in the comments, redna xelafx has been fixed as follows: "CPython not only has stacks but also has native stack and PyFrameObject ". Disclaimer: The Python implementation mentioned below or related detailed concepts refer to CPython.

1. first, solve the questions in the demo code of the subject.
(I have never written compiler) whether it is Python or C, a very important concept is name and object. In Python, the value assignment statement has a very simple abstract concept, as follows:
Assignment expression in Python have two things in common:
      • Create a new object
      • Build a connection between that new object and a name
The subject can think that since a = 1 is assignment expression in Python, is def func (): pass a value assignment statement? (Please keep up with the two concepts of the value assignment expression above) I didn't talk about the problem, but I had some doubts about the assignment in the demo code to help the subject understand more deeply.

On the one hand, in order to make better use of the efficient memory, on the other hand, to release the programmer from the details of various memory management, better focus on "business logic ", with GC and some nice technologies, such as reference

In [8]: string_1 = "EOF"In [9]: string_2 = "EOF"In [11]: print "address 1: %x" % id(string_1)address 1: 7f2517898530In [12]: print "address 2: %x" % id(string_2)address 2: 7f2517898530
Do not use the Java idea to understand python. The compiler does not understand. Do not post comments.
But the string is a constant in python, and the implementation is probably a constant pool or something like that.
The str value cannot be changed. only the address indicated by the variable flag can be changed.

a = "str"b = "str"a is b
Remember: CPython is used as an example. all variables are a PyObject *, which is a pointer. all value assignment statements assign values to the address of an object, not just variables, the container contains items like list and dict elements, so the code here is equivalent:
PyObject * a = new_str ("abc ");
PyObject * B =;
A = new_str ("xyz ");
Of course, the new_str function is not used internally. I forgot the specific name.

I used to share python references in a group. some people sorted out the chat records:
Reference of python objects-teaching records in Group 20130223 of python2
Let's take a look. welcome to the discussion and see this question, for example, what you want to say, without knowing what to say ....
1. A and B are two different variables. a = B is a variable assignment, not a reference assignment. Of course, the printed results are different. This is a problem with the python syntax.
2. Heap and stack. when the heap and stack are put together, of course it refers to the special heap and special stack in C language, rather than the stack in the data structure. So some answers are incorrect.
3. Python and python Virtual Machines. The python virtual machine is only an implementation of the python language. if the virtual machine is written in C, there are stacks and stacks. The problem is, is there a manual defining specific implementation methods?
4. If we talk about the python language itself, the question is which python book talks about the heap and stack. I mean, the implementation part of the language.
So, this question... Juvenile, please clarify the basic concepts and discuss them again. A and B should be just a label

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.