5.32 如何动态创建函数? ======================= .. image:: http://image.iswbm.com/20200804124133.png 在下面的代码中,每一次 for 循环都会创建一个返回特定字符串的函数。 .. code:: python from types import FunctionType for name in ("world", "python"): func = FunctionType(compile( ("def hello():\n" " return '{}'".format(name)), "", "exec").co_consts[0], globals()) print(func()) 输出如下 :: world python