Dynamic class construction?
Adrian Eyre
a.eyre at optichrome.com
Mon Jan 31 05:19:57 EST 2000
> def newClass(name, ops, superclass):
> class NewClass(superclass):
> pass
> for op in ops.keys():
> setattr(NewClass, op, ops[op])
> NewClass.__name__ = name
> return NewClass
>
> def anOp(self):
> return 'This is an instance of the class %s' % \
> self.__class__.__name__
>
> someOps={'__str__':anOp}
> G=newClass('G', someOps, Group) # Group defined elsewhere
> print G
> <class __main__.G at f92a8>
There's already a module which does this...
import new
def anOp(self):
return 'This is an instance of the class %s' % \
self.__class__.__name__
someOps={'__str__':anOp}
G=new.classobj('G', (Group,), someOps) # Group defined elsewhere
--------------------------------------------
Adrian Eyre <a.eyre at optichrome.com>
http://www.optichrome.com
More information about the Python-list
mailing list