solving the metaclass conflict

Michele Simionato mis6 at pitt.edu
Mon Jun 9 10:08:07 EDT 2003


pje at telecommunity.com (Phillip J. Eby) wrote in message news:<25b5433d.0306081024.517d855e at posting.google.com>...
> mis6 at pitt.edu (Michele Simionato) wrote in message news:<2259b0e2.0306080334.329fa409 at posting.google.com>...
> class M1(type): pass
> class M2(type): pass
> class B1(object): __metaclass__ = M1
> class B2(object): __metaclass__ = M2
> class B3(B2): pass
> 
> class C1(B1,B2): __metaclass__ = funcThatMakesMetaclass
> class C2(B1,B3): __metaclass__ = funcThatMakesMetaclass
> 
> If I understand your code correctly, a new metaclass will be generated
> for C2, even though the metaclass generated for C1 would be adequate.

You are indeed correct and I have fixed this issue in the last version in
the cookbook, which fixes all the problems you pointed out until now (and
it is even shorter, under the 20 lines of code!).
Of course, there could be new ones, and I welcome your criticism ;)

> Anyway, *writing* it wasn't the hard part.  Getting it to be *correct*
> for all inputs was the hard part.  :)
> 

I couldn't agree more!
  
> Under Python 2.2, you can't mix a new-style class with 'type' (and not
> cause Python to core-dump when you inspect the resulting class!).  So
> if you want to create a mixin that can be used with 'type' as well as
> other kinds of classes, you *have* to use a classic class.

Really ? I did this kind of games without problems ...
In Python 2.2.0 (in 2.3 too) the following works:

class N(object): pass # new style
class M(N,type): pass # type
help(M) # this inspects the metaclass

Can you give my an example of core-dump, just for curiosity sake?

Thanks again for the feedback,

                                              Michele




More information about the Python-list mailing list