第一件事就是把名字正确化:
>>> def increment(obj):
... obj.count += 1
...
>>> class A(object):
... def __init__(self):
... self.count = 0
...
>>> o = A()
>>> o.__init__
<bound method A.__init__ of <__main__.A object at 0x0000000002766EF0>>
>>> increment
<function increment at 0x00000000027797C8>
正确的名称是 < 强力 > 函数 < / 强力 > 和 < 强力 > 受约束的方法 < / 强力 > 。 现在你可以寻找如何 < a href=" https:// stackoverflow.com/ a/ 10154/05/ 1149736" @ em > Bind 一种未受约束的方法 em_ / / a >, 你可能会最后读到 < a href=" https://docs. python. org/3/howto/ descripor. html" rel=" nofollown noreferrer" @em> descritors :
In general, a descriptor is an object attribute with "binding
behavior", one whose attribute access has been overridden by methods
in the descriptor protocol. Those methods are __get__
, __set__
, and
__delete__
. If any of those methods are defined for an object, it is said to be a descriptor.
您可以使用不同的调用
>>> increment.__get__(None, type(None))
<function increment at 0x00000000027797C8>
>>> increment.__get__(o, type(o))
<bound method A.increment of <__main__.A object at 0x00000000027669B0>>
它的工作就像一个魅力:
>>> o = A()
>>> increment.__get__(None, type(None))(o)
>>> o.count
1
>>> increment.__get__(o, type(o))()
>>> o.count
2
您可以很容易地将这些 新约束的方法 添加到对象上:
def increment(obj):
obj.count += 1
def addition(obj, number):
obj.count += number
class A(object):
def __init__(self):
self.count = 0
o = A()
o.inc = increment.__get__(o)
o.add = addition.__get__(o)
print(o.count) # 0
o.inc()
print(o.count) # 1
o.add(5)
print(o.count) # 6
或者创建您自己的 < em> 描述符 < / em >, 将 < em> 函数 < / em > 转换为 < em > 受约束方法 < / em > :
class BoundMethod(object):
def __init__(self, function):
self.function = function
def __get__(self, obj, objtype=None):
print( Getting , obj, objtype)
return self.function.__get__(obj, objtype)
class B(object):
def __init__(self):
self.count = 0
inc = BoundMethod(increment)
add = BoundMethod(addition)
o = B()
print(o.count) # 0
o.inc()
# Getting <__main__.B object at 0x0000000002677978> <class __main__.B >
print(o.count) # 1
o.add(5)
# Getting <__main__.B object at 0x0000000002677978> <class __main__.B >
print(o.count) # 6
您也可以看到,这与“https://docs.python.org/3/howto/descriptionor.html#formations-and-methods” rel=“不跟随 noreferrer"_em > follow / bound 方法 原则 相当一致:
分类词典存储方法作为函数。 在分类定义中, 方法是使用用于创建函数的常用工具 def 和 lambda 来写入的。 与常规函数的唯一区别是, 第一个参数是保留给对象实例。 根据 Python Convention, 例引用被称为自我, 但可以称为此或其它变量名称 。
为了支持方法调用, 函数包括 属性存取期间绑定方法的 方法。 这意味着所有函数都是非数据描述符, 返回约束或非约束方法, 取决于从对象还是从类别中引用的方法 。
测试初始化时 功能 变成 受约束的方法 :
>>> B.add
# Getting None <class __main__.B >
<function addition at 0x00000000025859C8>
>>> o.add
# Getting <__main__.B object at 0x00000000030B1128> <class __main__.B >
<bound method B.addition of <__main__.B object at 0x00000000030B1128>>