problem in using metaclasses to inspect Python code

Michele Simionato mis6 at pitt.edu
Thu Dec 12 14:36:37 EST 2002


Greg Chapman <glc at well.com> wrote in message news:<60ahvuo3vrru0u9etkdoismti9op5pkel9 at 4ax.com>...

<snip beautiful solution>


The more I progress with Python, the more I think that is very easy on the
surface but yet very sophisticated under the hood (which is a good thing, BTW).

You are right that my original problem had nothing to do with metaclasses, so
I pose you a new one more on topic (there is no limit to my reserve 
of problems... ;-)

If I write

class Meta(type):
    def __init__(cls,name,bases,dict):
        print "%s called me!" % name

class C(object):
    __metaclass__=Meta

then the output of this program is what you expect:

C called me!

However, if I write

class D(object):
    pass

D.__metaclass__=Meta

I DON'T obtain what I expect, i.e. "D called me!": the metaclass
Meta is not automatically called. How do I invoke Meta explicitly ?

TIA for your patience with me...


                                         Michele



More information about the Python-list mailing list