How call arguments stack works in Python?
<p>I am learning python and trying to get idea how stack works in python.  I have some doubts. I know how stack work and i have read many articles and tutorials about it. I have read many threads on stackoverflow they are all good, still I have some doubts.</p>

<p>As far I have read stack stores and return values of function. It work by LIFO principal.</p>

<p>Only first value of the stack will return and store at top.</p>

<p>So my question is - suppose there are four variables:</p>

<pre><code>a = 9
b = 2
c = 13
d = 4
</code></pre>

<p>They are stored in stack in order in which function call the value, for example:</p>

<pre><code>sum = a + d
sum = b + c

a = 9
d = 4
b = 2
c = 13
</code></pre>

<p>and they will return from top to bottom. Now my confusion is - if any operation requires to use <code>d</code> and <code>c</code>, while stack return values from the top, how stack will get the values <code>d</code> and <code>c</code>, as long as they are in middle of stack. Will stack first return a and then return d ??</p>

<p>That's the confusion.</p>

<p>If you're interested how python works under the hood, there is a standard CPython library <a href="https://docs.python.org/2/library/dis.html" rel="nofollow">dis</a>.
Here is the output for your test code:</p>

<pre><code>&gt;&gt;&gt; import dis
&gt;&gt;&gt; def test():
...     a = 9
...     b = 2
...     c = 13
...     d = 14
...     sum1 = a + d
...     sum2 = b + c
... 
&gt;&gt;&gt; dis.dis(test)
  2           0 LOAD_CONST               1 (9)
              3 STORE_FAST               0 (a)

  3           6 LOAD_CONST               2 (2)
              9 STORE_FAST               1 (b)

  4          12 LOAD_CONST               3 (13)
             15 STORE_FAST               2 (c)

  5          18 LOAD_CONST               4 (14)
             21 STORE_FAST               3 (d)

  6          24 LOAD_FAST                0 (a)
             27 LOAD_FAST                3 (d)
             30 BINARY_ADD          
             31 STORE_FAST               4 (sum1)

  7          34 LOAD_FAST                1 (b)
             37 LOAD_FAST                2 (c)
             40 BINARY_ADD          
             41 STORE_FAST               5 (sum2)
             44 LOAD_CONST               0 (None)
             47 RETURN_VALUE        
</code></pre>

<p>and it has nothing to do with stack as you can see.
It won't do even with adjusted code, like</p>

<pre><code>&gt;&gt;&gt; def test2(a,b,c,d):
...     sum1 = a + d
...     sum2 = b + c
... 
&gt;&gt;&gt; dis.dis(test2)
  2           0 LOAD_FAST                0 (a)
              3 LOAD_FAST                3 (d)
              6 BINARY_ADD          
              7 STORE_FAST               4 (sum1)

  3          10 LOAD_FAST                1 (b)
             13 LOAD_FAST                2 (c)
             16 BINARY_ADD          
             17 STORE_FAST               5 (sum2)
             20 LOAD_CONST               0 (None)
             23 RETURN_VALUE        
</code></pre>

<p>If you're interested in stack concept in general in computing, you should switch to some low-level (2.5 generation languages, like C), or go even deeper to assembly languages.</p>

<p>Reading about various calling conventions may be another good start point as well ( <a href="https://en.wikipedia.org/wiki/X86_calling_conventions" rel="nofollow">x86 calling conventions</a> for example )</p>

python stack callstack