Function mistaken for a method
John Machin
sjmachin at lexicon.net
Thu Jun 1 08:03:28 EDT 2006
On 1/06/2006 9:46 PM, Maric Michaud wrote:
> Le Jeudi 01 Juin 2006 13:34, Peter Otten a écrit :
>> A python-coded function has a __get__ attribute, a C-function doesn't.
>> Therefore C1.f performs just the normal attribute lookup while C2.f also
>> triggers the f.__get__(C2(), C2) call via the descriptor protocol which
>> happens to return a bound method.
> I don't think it's about c-coded versus python-coded stuff, C1.f is a type,
> C2.f is a method.
>
Try putting f = chr (a C function); it behaves like int, not like a
1-arg Python function. See below.
Cheers,
John
C:\junk>type func_meth.py
class C:
f = None
def __init__(self):
if self.f is not None:
self.x = self.f(0)
else:
self.x = 99 # differs from int(0) :-)
class C1(C):
f = int
class C2(C):
def f(self, arg):
return arg != 0
class C3(C):
pass
class C4(C):
f = chr
for cls in (C1, C2, C3, C4):
o = cls()
print "callable: %r; result: %r" % (o.f, o.x)
C:\junk>func_meth.py
callable: <type 'int'>; result: 0
callable: <bound method C2.f of <__main__.C2 instance at 0x00AE6F58>>;
result: False
callable: None; result: 99
callable: <built-in function chr>; result: '\x00'
C:\junk>
More information about the Python-list
mailing list