mro() or __mro__?

Peter Otten __peter__ at web.de
Sat Oct 23 11:15:15 EDT 2010


Hrvoje Niksic wrote:

> The documentation of the mro() method on the class object says:
> 
> class.mro()
>     This method can be overridden by a metaclass to customize the method
>     resolution order for its instances. It is called at class
>     instantiation, and its result is stored in __mro__.
> 
> Am I interpreting it correctly that the "__mro__" attribute is intended
> to be used directly, while the "mro" method is intended to be optionally
> overridden by the metaclass implementor?

Looks like you're right:

>>> class T(type):
...     def mro(self):
...             r = type.mro(self)
...             print r
...             return r[::-1]
...
>>> class A:
...     __metaclass__ = T
...
[<class '__main__.A'>, <type 'object'>]
>>> A.__mro__
(<type 'object'>, <class '__main__.A'>)

 
> If that is the case, it sounds exactly the opposite to the normal python
> convention that __methods__ are part of the protocol, and
> underscore-less functions or methods are meant to be called by the end
> user of the class.

While I'm not aware of any cases where

obj.foo() translates to obj.__foo__() 

internally make_mro() might still have been a more appropriate name.

Peter



More information about the Python-list mailing list