Ask HN: How does the return statement of a function work?
To expand on blindriver&#x27;s response, the standard C-based implementation of Python (&quot;CPython&quot;) uses a stack-based assembly language for what&#x27;s called the &quot;Python virtual machine&quot;.<p>In the PVM, objects on the stack are owned by the stack. The implementation carefully ensures that the reference count of those stack-owned objects is correct. That&#x27;s how the return object is not freed when the local context finishes - the stack owns it.<p>You can look at the CPython assembly language with the &quot;dis&quot; module:<p><pre><code>    def func(a, b):
        return a + b

  &gt;&gt;&gt; from t import  *
  &gt;&gt;&gt; dis.dis(func)
    2           0 LOAD_FAST                0 (a)
                2 LOAD_FAST                1 (b)
                4 BINARY_ADD
                6 RETURN_VALUE
</code></pre>
The &quot;LOAD_FAST&quot; tells the PVM to get the value of the &quot;a&quot; local variable and put it on the stack. More specifically, we can look at the implementation of &quot;LOAD_FAST&quot; at <a href="https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;cpython&#x2F;blob&#x2F;main&#x2F;Python&#x2F;generated_cases.c.h#L34">https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;cpython&#x2F;blob&#x2F;main&#x2F;Python&#x2F;generated...</a> (which has is derived from <a href="https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;cpython&#x2F;blob&#x2F;main&#x2F;Python&#x2F;bytecodes.c#L149">https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;cpython&#x2F;blob&#x2F;main&#x2F;Python&#x2F;bytecodes...</a> )<p><pre><code>        TARGET(LOAD_FAST_CHECK) {
            PyObject *value;
            #line 150 &quot;Python&#x2F;bytecodes.c&quot;
            value = GETLOCAL(oparg);
            if (value == NULL) goto unbound_local_error;
            Py_INCREF(value);
            #line 41 &quot;Python&#x2F;generated_cases.c.h&quot;
            STACK_GROW(1);
            stack_pointer[-1] = value;
            DISPATCH();
        }
</code></pre>
This gets the local value from &quot;a&quot;. If it doesn&#x27;t exist, raise an UnboundLocalError. If it does exist, increase the reference count, increase the stack size by one, and put the new value on end of the stack. The DISPATCH() means to go to the next instruction.<p>The BINARY_ADD implementation has a number of specializations. If a and b are both integers, we can see the implementation at <a href="https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;cpython&#x2F;blob&#x2F;main&#x2F;Python&#x2F;generated_cases.c.h#L442">https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;cpython&#x2F;blob&#x2F;main&#x2F;Python&#x2F;generated...</a> :<p><pre><code>        TARGET(BINARY_OP_ADD_INT) {
            PyObject *right = stack_pointer[-1];
            PyObject *left = stack_pointer[-2];
            PyObject *sum;
            #line 323 &quot;Python&#x2F;bytecodes.c&quot;
            assert(cframe.use_tracing == 0);
            DEOPT_IF(!PyLong_CheckExact(left), BINARY_OP);
            DEOPT_IF(Py_TYPE(right) != Py_TYPE(left), BINARY_OP);
            STAT_INC(BINARY_OP, hit);
            sum = _PyLong_Add((PyLongObject *)left, (PyLongObject *)right);
            _Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
            _Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
            if (sum == NULL) goto pop_2_error;
            #line 456 &quot;Python&#x2F;generated_cases.c.h&quot;
            STACK_SHRINK(1);
            stack_pointer[-1] = sum;
            next_instr += 1;
            DISPATCH();
        }
</code></pre>
This takes the last two elements on the stack (at positions -1 and -2), does some checks that these really are integers, computes the sum, does a call to the internal macro _Py_DECREF_SPECIALIZED (at the top of the file), which decrements the reference count and frees the object if the refcount is 0.<p><pre><code>        _Py_DECREF_STAT_INC(); \
        PyObject *op = _PyObject_CAST(arg); \
        if (--op-&gt;ob_refcnt == 0) { \
            destructor d = (destructor)(dealloc); \
            d(op); \
        }
</code></pre>
It can do this because it knows the object is an integer, so it knows the correct deallocator for it.<p>The _PyLong_Add returns the sum as an object with a refcount of at least 1, so there&#x27;s no need for an increment in the PVM implementation.<p>Since the last two objects have been deallocated, it removes those two from the stack, but it will place the new sum in the stack, giving an overall stack size shrink by one.<p>The sum is placed in the end of the stack, and execution goes on to the next instruction.