Generating modul classes with eval

Axel Straschil axel at straschil.com
Wed Feb 2 15:49:07 EST 2005


Hello!

I was fooling around with creating classes for a module with eval,
something like:

MyModule.py:

class Base:
	init(self, name):
		self._name = name

for myclass in ['A', 'B', 'C']:
	code="class %s(Base):\n\tinit(self, name='%s')\n\t\tsuper(%s,
	self).__init(name=name)\n"%dict(myclass, myclass.lower(), myclass())
	... codeop and eval stuff ...
a=A()
print a

that gives: <class '__main__.A'>, but I want MyModule.A ;-)

Can someone give me a hint how to create classes in a module with eval
and codeop so that they exist like the code was written in?

Thanks, 
AXEL.




More information about the Python-list mailing list