The main use of code objects is to separate the static parts of functions (code) from dynamic parts (functions). Code objects are things that are hidden in .pyc files and are created when the code is compiled; functional objects are created from them at runtime when functions are declared. They are displayed to reflect the debugger and often do not require direct use.
All languages ββthat support closure have something similar to them; they simply are not always exposed to the language, as they are in Python, which is more fully reflected than most languages.
You can use code objects to create function objects via types.FunctionType , but this very rarely has practical use - in other words, don't do this:
def func(a=1): print a func2 = types.FunctionType(func.func_code, globals(), 'func2', (2,)) func2()
source share