Suppose I create a module on the fly:
import imp
my_module = imp.new_module('my_module')
and I want to add n
similar names/value to this module, something that would be equivalent to
my_module.a1 = None
my_module.a2 = None
my_module.a3 = None
...
How can I access a module namespace like a dictionary (or in a similar way) so that I can write something like
for i in range(n):
my_module.some_env_like_dict[f"a{i}"] = None
Also how can I remove these names in a similar way?
I understand that Python may not have a recommended or official way of doing this. Obviously this is not for any serious project.
I'm looking for a solution that is more elegant than using exec
.