Say in a module I want to define:
a = 'a'
b = 'b'
...
z = 'z'
For some set (in this case I chose letters). How do I dynamically set attributes on the current module? Something like:
for letter in ['a', ..., 'z']:
setattr(globals(), letter, letter)
This doesn’t work, but what would? (Also my understanding is that globals() within a module points to a dict of the attributes of that module, but feel free to correct me if that’s wrong).
globals() returns the dictionary of the current module, so you add items to it as you would to any other dictionary. Try:
or to eliminate the repeated call to globals():
or even: