Python . - UnboundMethod ( , ).
( , ), Python UnboundMethod (.. , ).
UnboundMethod :
def myfunction(a, b):
return a + b
class A(object):
a = myfunction
A.a(1, 2)
. , :
A().a(1, 2)
, , (, getattr) , UnboundMethod, im_self (im_self im_func UnboundMethod). , , im_func. , im_func, , im_self. , (, ).
, Python :
- as-is,
@staticmethod. , UnboundMethod. , . - , ( , ), (INSTEAD of self: cls), , -
@classmethod.
:
class A(object):
a = staticmethod(lambda a, b: a + b)
A.a(1, 2)
A().a(1, 2)
.
:
def add_print(cls, a, b):
print cls.__name__
return a + b
class A(object):
ap = classmethod(add_print)
class B(A):
pass
A.ap(1, 2)
B.ap(1, 2)
A().ap(1, 2)
B().ap(1, 2)
.