I used to create a exec statement to generate a function to use in Python2. However, when I move to Python3, the same approach stopped work ing.
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def a():
... exec(compile("def printt(): print(2)","printt","exec"))
... printt()
...
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in a
NameError: name 'printt' is not defined
>>>
Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def a():
... exec(compile("def printt(): print(2)","printt","exec"))
... printt()
...
>>> a()
2
>>>