Function definition in Python
def foo(): pass
actually convenient for the programmer to say something like (pseudocode) *:
foo = function(code=compile('pass', ...), globals=globals(), name='foo')
therefore, the shell is simply in between:
foo = my_wrapper(function(...))
if the wrapper is a class, __init__ is called. if it is a function, it will be called. After this statement, everything works as usual.
* This pseudo code is not so far from the real code:
>>> def bar(): pass ... >>> body = compile('print("hello")', '', 'exec') >>> function = type(bar) >>> foo = function(body, globals(), 'foo') >>> foo() hello
source share