Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 新式类与旧式类 #31

Open
bicongwang opened this issue Mar 5, 2017 · 3 comments
Open

Python 新式类与旧式类 #31

bicongwang opened this issue Mar 5, 2017 · 3 comments

Comments

@bicongwang
Copy link

引用的文章在讨论新式类的MRO顺序。

在经典对象模型中,方法和属性的查找链是按照从左到右,深度优先的方式进行查找。所以当A的实例b
要使用属性a时,它的查找顺序为:A->B->D->C->A,这样做就会忽略类C的定义a,而先找到的基类D的
属性a,这是一个bug,这个问题在新式类中得到修复,新的对象模型采用的是从左到右,广度优先的方式
进行查找,所以查找顺序为A->B->C->D,可以正确的返回类C的属性a。

其中提到新式类是广度优先查找,这里明显错误。事实上Python在这里采用的是C3算法。

@sniperXue
Copy link

广度优先是不对的,只有某些情况下采用C3算法的结果和广度优先遍历的结果一样。C3算法解决单调性和本地优先级的问题才提出的。

@bicongwang
Copy link
Author

@sniperXue 嗯,是这样的。不过时隔一年作者也没有修正这个问题

@yunqingqing
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants