代码示例的完成方式使得有点难以分辨发生了什么。然而,这等同于:
child_methods = [] # a list of all the methods in `Child`
class Parent(object):
def parent_method(self):
print "parent_method() called"
method = child_methods[0]
method(self)
class Child(Parent):
def child_method(self):
print "child_method() called"
# add child_method to child_methods
child_methods.append(Child.child_method)
正如你所看到的,child_methods[0]
实际上是功能Child.child_method
,这是一个简单的功能,而不是一个绑定的方法。它与Child
的实例没有关联,这就是为什么你可以并且必须自己通过self
。你会从一个Child
实例获得一个绑定的方法有:
child_obj = Child()
bound_child_method = child_obj.child_method
这是由事实Python会查找属性中的对象的类型,如果他们没有在该实例中取得不清楚。例如:
# A dummy class to hold attributes
class Foo(object):
pass
Foo.bar = 123 # we're adding an attribute to the type itself
foo1 = Foo()
print foo1.bar # prints 123
foo1.bar = 456 # this `bar` attribute "hides" the one on the type
print foo1.bar # prints 456
foo2 = Foo()
print foo2.bar # prints the 123 from the type again
这就是为什么你的代码示例中,commands
真的是“全局”变量,它只是被通过的B
实例混淆的访问。 (如果只有B
或其子对象访问此变量,但查找规则是一个小问题,这不一定是坏习惯。)
谢谢。我明白“无约束的方法”需要通过“自我”。 –
准确。如果你添加一个* bound *方法到列表中,使用'child_methods.append(Child()。child_method)',那么这就没有必要了 - 它会使用你从它得到的'Child'实例。 – millimoose
这是不一样的。在问题的代码中,没有未绑定的方法访问。 'method'的值是一个常规函数。 – newacct