Creating arbitrary objects in the current module namespace

This short recipe will show how to create arbitrary named objects in the current module namespace. Either globals() or locals() can be used for the task, depending on the namespace where the variables have to be created (global or local).

>>> for i in range(10):
>>> globals()['test%s' % i] = i
>>> print test0
0
>>> print test1
1

Comments