Imposing metaclass on library

Michael Hudson mwh at python.net
Wed Dec 18 09:28:36 EST 2002


Lulu of the Lotus-Eaters <mertz at gnosis.cx> writes:

> I am having a problem with metaclass resolution order (I think).
> 
> Here is the situation:  I have a library of classes, and I do not want
> to modify the library.  But I would like to cause all -descendents- of
> library classes to be created with a custom metaclass (within my
> application that uses the library).
> 
> The library can look like (trivial case):
> 
>     % cat library.py
>     class Base1(object): pass
>     class Base2(object): pass
> 
>     class Base1(Base1): __metaclass__ = Meta

I think this, or something like it, is your only real hope.

>     class App7(Base1): pass     # Works with redefined Base1
>     class App8(Base1): pass     # ditto
> 
>     # Less forced looking attempt that fails.
>     Base2.__metaclass__ = Meta
>     class App9(Base2): pass     # But haven't done anything for Base2

__metaclass__ has to be around at class construction time.  This isn't
going to work.

Cheers,
M.

-- 
  "Also, does the simple algorithm you used in Cyclops have a name?"
  "Not officially, but it answers to "hey, dumb-ass!"
                       -- Neil Schemenauer and Tim Peters, 23 Feb 2001



More information about the Python-list mailing list