I was trying out using keywords encoded in a string by using exec() and eval() functions.
I got an error.
For this time the keywords I'm working with is import.
The problem seems to be related with scopes.
And so they did... got imported.
You can imagine my face when in the next row they were not recognised.
Could any smart guy or girl explain to me what has happened?
I got an error.
For this time the keywords I'm working with is import.
The problem seems to be related with scopes.
def imp3(*Names): for name in Names: exec("import "+name) print(eval(name)) #for debugging return tuple(eval(name) for name in Names) os,json,time= imp3("os","json","time")
Output:<module 'os' from 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\\lib\\os.py'>
<module 'json' from 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\\lib\\json\\__init__.py'>
<module 'time' (built-in)>
Error:Traceback (most recent call last):
File "G:\Builtins.py", line 25, in <module>
os,json,time= imp3("os","json","time")
File "G:\Builtins.py", line 23, in imp3
return tuple(eval(name) for name in Names)
File "G:\Builtins.py", line 23, in <genexpr>
return tuple(eval(name) for name in Names)
File "<string>", line 1, in <module>
NameError: name 'os' is not defined
I've added a print statement (as you can see), so that I'm sure that modules got indeed imported. 
And so they did... got imported.

You can imagine my face when in the next row they were not recognised.

Could any smart guy or girl explain to me what has happened?
