I can think of a couple ways to do this... In order of (what I believe to be) best to worst we have:
First, setting attributes on the current module
# The first way I had of grabbing the module:
mod = __import__(__name__, fromlist=['nonempty'])
# From Roger's suggestion:
import sys
mod = sys.modules[__name__]
for name in ['A', 'B', 'C']:
class_ = type(name, (object, ), {})
setattr(mod, name, class_)
print A, B, C
Second, setting into the current globals dict:
for name in ['A', 'B', 'C']:
class_ = type(name, (object, ), {})
globals()[name] = class_
print A, B, C
Last, using exec (eww):
for name in ['A', 'B', 'C']:
class_ = type(name, (object, ), {})
exec "%s = class_" % name
print A, B, C
I have tested all three of these work in a stand alone script (where __name__ == "__main__"
) and as a module in a larger package.
EDIT: With regards to the discussion of method 1 vs method 2 in the comments, they both do EXACTLY the same thing. The name space of a module is defined in a dict stored on the module (here are the docs). From module level you can get this via globals()
and from outside you can access it via attributes, or the __dict__
attribute of the module.
An interactive session to demonstrate:
Python 2.6.4 (r264:75706, Nov 8 2009, 17:35:59)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> mod = sys.modules[__name__]
>>> mod.__dict__ is globals()
True