Unexpected __metaclass__ method behavior

anne.nospam01 at wangnick.de anne.nospam01 at wangnick.de
Sun Dec 30 07:00:26 EST 2007


Dear fellow Pythonians,

I just stumbled upon the following unexpected behavior:

class TestType(type):
    def Foo(self): return 'TestType Foo'
class Test(object):
    __metaclass__ = TestType
    def Foo(self): return 'Test Foo'
t = Test()
print t.Foo()
print Test.Foo()

This will produce:
Test Foo
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print Test.Foo()
TypeError: unbound method Foo() must be called with Test instance as
first argument (got nothing instead)

I can imagine why this is happening, and that there is no easy
solution, but it is not what I was expecting.

Anybody willing to explain the details of what's exactly going on
during the method lookup of Test.Foo?

Kind regards,
Sebastian



More information about the Python-list mailing list