Q: Meta-class usage

Michele Simionato michele.simionato at poste.it
Sat Dec 13 06:55:54 EST 2003


google at daishi.fastmail.fm (daishi) wrote in message news:<d22692a3.0312121645.7f99cfd1 at posting.google.com>...
> Now based on http://www.python.org/2.3/mro.html I believe this has to
> do with the fact that in the first case of NamedFoo we have
>   merge([object], [NamedClass, object], [object, NamedClass])
> (as opposed to
>   merge([NamedClass, object], [object], [NamedClass, object])
> when the modification to bases is changed)
> 
> And the SubNamedFoo error arises because of
>   merge([NamedClass, object], [NamedFoo, NamedClass, object],
>     [NamedClass, NamedFoo, object])
> 
> Is this correct?
> (I just want to make sure that I am understanding what's going on
> correctly.)

Correct. More specific classes should go first to avoid confusion
about the precedence order.

> The basic idea of what I'm trying to do is (I think) fairly simple.
> I have several fairly similar class definitions that I want, and I
> was hoping to use metaclasses to make their definition more compact.
> (As comparison, I would use a #define in C or a defmacro in Lisp).
> I would like to use these classes once defined as if they were
> regularly defined classes, included subclassing. My initial attempt
> at subclassing with subclass class SubNamedFoo(NamedFoo) failed
> because of the fact that since the metaclass of SubNamedFoo inherits
> from NamedFoo and hence becomes NamedType, and the NamedType.__init__
> is executed for SubNamedFoo also, which I didn't want - I would like
> SubNamedFoo to be oblivious of the fact that NamedFoo was defined
> using a customized metaclass. I was wondering what the ideal way to
> achieve this might be.
>  

Maybe a simple function acting as a class factory? Sometimes the
simplest solutions are the best solutions. So, unless you anticipate
the need to use inheritance of metaclasses, why don't you use a simple
function return a class (created with type(name,bases,dic), the basic
metaclass) ?

  Michele




More information about the Python-list mailing list