Hi,
This sort of code works for staticmethods, but fails with instance methods.
Why does it fail? O:-)
[color=blue][color=green][color=darkred]
>>> class C (object):[/color][/color][/color]
def g(x,y):
print 'static g', x, y
g = staticmethod(g)
[color=blue][color=green][color=darkred]
>>> def h(self, x,y):[/color][/color][/color]
return x+y
[color=blue][color=green][color=darkred]
>>> C.h = h
>>> dir(C)[/color][/color][/color]
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute __',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__' , '__repr__', '__setattr__', '__str__', '__weakref__', 'g',
'h'][color=blue][color=green][color=darkred]
>>> C.h(3,4)[/color][/color][/color]
Traceback (most recent call last):
File "<pyshell#6 2>", line 1, in -toplevel-
C.h(3,4)
TypeError: unbound method h() must be called with C instance as first argument
(got int instance instead)
This sort of code works for staticmethods, but fails with instance methods.
Why does it fail? O:-)
[color=blue][color=green][color=darkred]
>>> class C (object):[/color][/color][/color]
def g(x,y):
print 'static g', x, y
g = staticmethod(g)
[color=blue][color=green][color=darkred]
>>> def h(self, x,y):[/color][/color][/color]
return x+y
[color=blue][color=green][color=darkred]
>>> C.h = h
>>> dir(C)[/color][/color][/color]
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute __',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__' , '__repr__', '__setattr__', '__str__', '__weakref__', 'g',
'h'][color=blue][color=green][color=darkred]
>>> C.h(3,4)[/color][/color][/color]
Traceback (most recent call last):
File "<pyshell#6 2>", line 1, in -toplevel-
C.h(3,4)
TypeError: unbound method h() must be called with C instance as first argument
(got int instance instead)
Comment