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

Bruno Desthuilliers onurb at xiludom.gro
Fri Jun 23 11:31:00 CEST 2006


sashang at gmail.com wrote:
> Hi
> 
> I'd like to use metaclasses to dynamically generate a class based on a
> parameter to the objects init function.

Do you really need a metaclass for this ?

> For example:
> 
> class MetaThing(type):
>     def __init__(cls, name, bases, dict, extra_information):
>         super(MetaThing, cls).__init__(name, bases, dict)
>         #setup the class based on the parameter extra_information
> 
> class Thing:
>     __metaclass__ = MetaThing
>     def __init__(self, extra_information):
>          #Somehow pass extra_information to the MetaThing
> 
> extra_information = 1
> t = Thing(extra_information)

Why would you want a new *class* here ?

> The above sample won't work but I hope it demonstrates what I'm trying
> to do.

Not enough, I'm afraid - unless it's just me being dumb. From what I see
here, you just can add the extra informations on the object in the
initializer. What's your *real* use case ?




-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list