Trouble with metaclass

Alex Martelli aleax at aleax.it
Thu Nov 13 08:38:56 EST 2003


anton muhin wrote:

> Fernando Rodriguez wrote:
   ...
>> class MetaChecker(type):
>>     def __new__(cls, name, bases, attribs):
>>         for name, value in attribs.iteritems():
>>             if  inspect.ismethod(value):
   ...
> The problem seems to be with ismethod function. Cf.:

Right, or, more precisely, there is no problem with ismethod -- it
correctly reports that the values in the attribs dictionary are not
method objects (they're function objects).

Summarizing and simplifying only a little bit...:

1. The def statement always and exclusively creates a function object.

2. A function object is also a descriptor: when you call f.__get__(x),
   you get back a method object with im_func == f and im_self == x.

3. The attribute lookup process calls x.__get__(self) on any attribute
   it may find in the class's dictionary.

But at this point in the execution of the metaclass's __new__, there
is no "class dictionary" yet -- there isn't even a _class_ -- just a
plain dictionary of attributes which will BECOME the class dictionary
once you call type.__new-_ with that dict as the 4th argument.


Alex





More information about the Python-list mailing list