Unexpected __metaclass__ method behavior

G

Guest

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
 
M

Michele Simionato

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?

The regular method is checked for *before* the metaclass method.
You must use

type(Test).Foo(Test)

to call the method. It is clear that it must be that way: when you do
(for instance)
SomeClass.__init__ you do not expect to have type.__init__(SomeClass)
called.
Notice that *all* classes have a metaclass, by default "type" for new-
style
classes and "ClassType" for old-style ones.

Michele Simionato
 
T

Terry Reedy

| 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.

Regardless of which Foo you expect to be called, both require an instance
argument to be bound to the paramenter 'self'.

print Test.Foo(t) # will print same as t.Foo()

tjr
 
G

Guest

Well, you see, I have some database functions that deal with "things"
which are either classes or instances thereof. I though polymorphism
would be a nice way to handle them identically, like:

def do(thing): thing.Foo()
do(t)
do(Test)

But never mind, I now understand that Test.__dict__ can contain only
one entry for 'Foo', and that this must be matched.

Kind regards,
Sebastian
 
A

Arnaud Delobelle

Well, you see, I have some database functions that deal with "things"
which are either classes or instances thereof. I though polymorphism
would be a nice way to handle them identically, like:

def do(thing): thing.Foo()
do(t)
do(Test)

But never mind, I now understand that Test.__dict__ can contain only
one entry for 'Foo', and that this must be matched.

Kind regards,
Sebastian

Of course you can do this. The trick is *not* to use metaclasses!

class Bar(object):
def foo(self): return 'instance foo'
@classmethod
def classfoo(cls): return 'class foo'

def do(x):
if isinstance(x, type):
return x.classfoo()
else:
return x.foo()

Then:
'class foo'

HTH
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Well, you see, I have some database functions that deal with "things"
which are either classes or instances thereof. I though polymorphism
would be a nice way to handle them identically, like:

def do(thing): thing.Foo()
do(t)
do(Test)

But never mind, I now understand that Test.__dict__ can contain only
one entry for 'Foo', and that this must be matched.

You may want to have a look at FormEncode's "declarative" API, with
particular attention to the 'classinstancemethod' stuff.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top