Generating modul classes with eval
Axel Straschil
axel at straschil.com
Thu Feb 3 07:59:10 EST 2005
Hello!
> After the loop has finished, the global variable TmpClass will be bound to
> whatever class was created last, and the variable class_base will be bound
> to that the base class of that same TmpClass. Therefore only this last class
> is guaranteed to work as expected.
Great, now it workes!
_module_name = sys.modules[__name__]
def _tag_class_factory(name, base):
class T(base):
def __init__(self, **props):
super(T, self).__init__(name=name.lower(), **props)
setattr(_module_name, name, T)
class_dic = {}
class_dic['Br'] = _Tag
class_dic['Hr'] = _Tag
class_dic['Html'] = _ContainerTag
class_dic['Table'] = _ContainerTag
class_dic['Td'] = _ContainerTag
class_dic['Tr'] = _ContainerTag
for name, base in class_dic.items():
_tag_class_factory(name, base)
print Table(contents=[Tr(contents=[Br()])])
gives: <table><tr><br/></tr></table>
Thanks,
AXEL.
More information about the Python-list
mailing list