On 26 June 2013 15:46, Peter Otten <__pete...@web.de> wrote: > The clean way to > cope with the situation is to use a dict: > > classnames = ["Vspace", ...] > classes = {name: type(name, ...) for name in classnames} > > Then you can access the Vspace class with > > classes["Vspace"] > > If that is inconvenient for your usecase you can alternatively update the > global (module) namespace: > > globals().update((name, type(name, ...) for name in classnames) > > For example: > >>>> class A(object): pass > ... >>>> globals().update((n, type(n, (A,), {})) for n in ["Beta", "Gamma"]) >>>> Beta > <class '__main__.Beta'> >>>> issubclass(Beta, A) > True
I would say if a dict isn't good, there are still some cases where you might not want to use globals. I _might_ do: import sys from types import ModuleType # As before newclasses = ['Vspace', 'Boldpath', "and so on", "and yes, these all become variables"] little_classes = {name: type(name, (int,), {}) for name in newclasses} # Make a module module_for_little_classes = ModuleType("module_for_little_classes", "All the things") module_for_little_classes.__dict__.update(little_classes) # Hack it in there! sys.modules["module_for_little_classes"] = module_for_little_classes # Now we can undo all import module_for_little_classes as mlc mlc.Vspace mlc.Boldpath ... # And undo all our hard work avoiding globals(): from module_for_little_classes import * Vspace Boldpath So, why avoid globals()? 1) Linters don't like globals() 2) Urm... it's ugly? 3) Ur....... -- http://mail.python.org/mailman/listinfo/python-list