Using metaclassed to dynamically generate a class based on a parameter to the objects init function.

Michele Simionato michele.simionato at gmail.com
Fri Jun 23 07:28:38 EDT 2006


sashang at gmail.com wrote:
> However I do think the solution to my problem lies with
> them since I have to dynamically generate a class and metaclasses
> provide a mechanism for doing this.

You rarely need a custom metaclass to generate classes. A class factory

def makeclass(classname, *attributes):
   cls = type(classname, mybases, mydic)
   for name, value in attributes:
      setattr(cls, name, attr)
   return cls

is the typical solution for your use case.

OTOH, if you are looking for use classes for metaclasses, look at the
Python Wiki
and use Google. 

   Michele Simionato




More information about the Python-list mailing list