首页 > 编程语言 > 详细

How to know MRO in python2

时间:2020-02-01 12:58:26      阅读:66      评论:0      收藏:0      [点我收藏+]

In python3, we can use ‘__mro__‘ to get the method-resolution-order like below:

>>> class a:
...     pass
...  
>>> a.__mro__
(<class ‘__main__.a‘>, <class ‘object‘>)
>>>

Does python2 has an alternative method for this? I tried to find one but failed.

>>> class a:
...     pass
... 
>>> dir(a)
[‘__doc__‘, ‘__module__‘]
>>> class a(object):
...     pass
... 
>>> dir(a)
[‘__class__‘, ‘__delattr__‘, ‘__dict__‘, ‘__doc__‘, ‘__format__‘, ‘__getattribute__‘, ‘__hash__‘, ‘__init__‘, ‘__module__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘__weakref__‘]

Answer

mro() is available since Python 2.2 (https://www.python.org/download/releases/2.3/mro/)

However the class must be a new style class.

>>> class A: pass
>>> A.mro()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class A has no attribute ‘mro‘

>>> class A(object): pass
>>> A.mro()
[<class ‘__main__.A‘>, <type ‘object‘>]

 

 

How to know MRO in python2

原文:https://www.cnblogs.com/qiumingcheng/p/12248057.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!