属性查找小练习:
1
class
Foo:
2
def
f1(self):
3
print(‘from Foo.f1‘)
4 5def f2(self):
6print(‘from Foo.f2‘)
7 self.f1()
8 910class Bar(Foo):
11def f2(self):
12print(‘from Bar.f2‘)
131415 b = Bar()
16b.f2()
1718结果为:
1920from Bar.f2
稍作修改:
1
class
Foo:
2
def
f1(self):
3
print(‘from Foo.f1‘)
4 5def f2(self):
6print(‘from Foo.f2‘)
7 self.f1()
8 910class Bar(Foo):
11def f1(self):
12print(‘from Bar.f2‘)
131415 b = Bar()
16b.f2()
1718结果为:
19from Foo.f2
20from Bar.f2
可以看出,属性的查找顺序为先从对象自身查找,然后从对象所在的类进行查找,然后从父类查找,依次查找,直至找到或者报错没有找到,
原文:https://www.cnblogs.com/xudachen/p/8592644.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!