TypeError: Cannot create a consistent method resolution order (MRO) for bases object

digitalorganics at gmail.com digitalorganics at gmail.com
Mon Jun 26 22:55:18 EDT 2006


Maric Michaud wrote:
> Le lundi 26 juin 2006 20:06, digitalorganics at gmail.com a écrit :
> > What are the reason one would get this error: TypeError: Cannot create
> > a consistent method resolution order (MRO) for bases object ??
> >
> > I can provide the code if needed....
> This is the python solution to the diamond problem (cf. wikipedia).
>
> In [61]: class a(object) : pass
>    ....:
> In [62]: class b(a) : pass
>    ....:
> In [63]: class c(object, a) : pass
>    ....:
> ---------------------------------------------------------------------------
> exceptions.TypeError                                 Traceback (most recent
> call last)
>
> /home/maric/<ipython console>
>
> TypeError: Error when calling the metaclass bases
>     Cannot create a consistent method resolution
> order (MRO) for bases object, a
>
> In [64]: b.mro()
> Out[64]: [<class '__main__.b'>, <class '__main__.a'>, <type 'object'>]
>
> In [65]: class c(a, object) : pass
>    ....:
>
> In [66]: c.mro()
> Out[66]: [<class '__main__.c'>, <class '__main__.a'>, <type 'object'>]
>
> In [67]: class d(b, c) : pass
>    ....:
>
> In [69]: d.mro()
> Out[69]:
> [<class '__main__.d'>,
>  <class '__main__.b'>,
>  <class '__main__.c'>,
>  <class '__main__.a'>,
>  <type 'object'>]
>
> mro means "method resolution order", this is the path the interpreter will
> look for attributes for a given class. You cannot introduce inconsistency in
> this path, for example duplicate the type object before another type (or any
> type wich appear to be the ancestor of another).
>
> --

Ah, thank you Maric, I see the problem. I diagrammed the flow of class
inheritance and I can see the problem clearly. Ruby doesn't have this
problem because it uses a single inheritance model complemented by the
power of mixins (modules of functionality mixed into classes as
needed). So what's the Pythonic solution to this problem?




More information about the Python-list mailing list