I'm practicing the alternative way of defining functions in Python, using FunctionType. I can create this way a simple function that prints hello world
:
from types import FunctionType
foo = FunctionType(compile("print('hello world')", '', 'exec'), globals())
foo()
This function does not take positional arguments:
from types import FunctionType
foo = FunctionType(compile("print('hello world')", '', 'exec'), globals())
foo('bar')
Traceback (most recent call last):
File "C:/Users/M/AppData/Local/Programs/Python/Python37/test_FunctionType.py", line 5, in <module>
foo('bar')
TypeError: <module>() takes 0 positional arguments but 1 was given
How to use FunctionType to define a function that takes positional arguments?