mro() or __mro__?

H

Hrvoje Niksic

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?

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.
 
P

Peter Otten

Hrvoje said:
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:
.... def mro(self):
.... r = type.mro(self)
.... print r
.... return r[::-1]
........ __metaclass__ = T
....
<class '__main__.A'>) said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top