Stack Chart - Python

I am trying to fully understand the principle of "Stack Diagram" . Can someone check if I'm right, please?

So far, I understand that the stack diagram is used to track the variables used in functions inside the code.

However, I'm not sure if a new variable is created in a specific function if it is included in the stack diagram.

For example, should I include the variable "p" in the stack diagram? Let them talk:

def g(y): p = A(y, y) print z, p return p def A(x, y): x = x + 1 return x * y x = 1 y = x + 2 

I think my stack should look something like this:

 <module> x --> 1 y --> 3 (Should I put 3 or x + 2 here) fct gy --> 3 (should I stop here or should I include a line for the variable p) fct A x --> 4 y --> 3 

Last question: is it worth mentioning anything about what other things the function performs. As with function A, it returns x * y = 12. Should we include this in the stack diagram or the diagram correctly, how is it?

thanks

+4
source share
1 answer

According to the definition of the stack diagram:

Each function is represented by a frame. A frame is a box with the name of the function next to it and the parameters and variables inside it.

This means that you also need to consider the variables defined in the inside . It’s better to store the y value as y=3 instead of y=x+2 - since we track the values ​​of the variables and are usually not related to how these values ​​were obtained

To answer the second request, the description shows that the stack diagram is used to track variables in the function name. However, remember that when developing language processors and runtime, they make the necessary additions to the proposed principle of the stack scheme, which makes it easy or efficient to process variables or identify errors.

I hope this post clarifies your doubts.

+3
source

Source: https://habr.com/ru/post/1495648/


All Articles