Python: infos about the implementation of a Python function

Viewed 121

I'm discovering the CPython implementation, the structure of Python objects and the Python bytecodes.

Playing with functions, I've found out that empty functions have a stack size of 1.
Why? What var is declared to occupy the stack space?


Empty function:

def empty():
    pass

Function infos:

>>> dis.show_code(empty)

Name:                empty
Filename:            <pyshell#27>
Argument count:      0
Kw-only arguments:   0
Stack size:          1
Number of locals:    0
Variable names:
Constants:
    0: None

Names:
Flags:               OPTIMIZED, NEWLOCALS, NOFREE
First line number:   1
Free variables:
Cell variables:



Function with locals:

def withlocals():
    first = 0
    second = [1, 2, 3]

Function infos:

>>> dis.show_code(withlocals)

Name:                withlocals
Filename:            <pyshell#27>
Argument count:      0
Kw-only arguments:   0
Stack size:          3
Number of locals:    2
Variable names:
    0: first
    1: second

Constants:
    0: None
    1: 0
    2: 1
    3: 2
    4: 3

Names:
Flags:               OPTIMIZED, NEWLOCALS, NOFREE
First line number:   1
Free variables:
Cell variables:
1 Answers
Related