Multiple inheritance : waht does this error mean ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jnair@ensim.com

    #1

    Multiple inheritance : waht does this error mean ?

    I am using Python 2.4.3
    [color=blue][color=green][color=darkred]
    >>>class K(object,list):[/color][/color][/color]
    ...: pass
    ...:
    ------------------------------------------------------------
    Traceback (most recent call last):
    File "<console>" , line 1, in ?
    TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
    order (MRO) for bases object, list


    Regards
    Jitendra Nair

  • Ben Finney

    #2
    Re: Multiple inheritance : waht does this error mean ?

    jnair@ensim.com writes:
    [color=blue][color=green][color=darkred]
    > >>>class K(object,list):[/color][/color]
    > ...: pass
    > ...:
    > ------------------------------------------------------------
    > Traceback (most recent call last):
    > File "<console>" , line 1, in ?
    > TypeError: Error when calling the metaclass bases
    > Cannot create a consistent method resolution
    > order (MRO) for bases object, list[/color]

    The class 'object' is already the base class of 'list', and of every
    other basic type.

    Python is saying that you need to decide what you want; inheritance
    from 'object', or inheritance from something that already inherits
    from 'object'.

    --
    \ "People come up to me and say, 'Emo, do people really come up |
    `\ to you?'" -- Emo Philips |
    _o__) |
    Ben Finney

    Comment

    • John Machin

      #3
      Re: Multiple inheritance : waht does this error mean ?

      Question for you: what do you think that "class K(object,list): "
      means?? What are you trying to achieve? What do you plan to do with K?
      If you want it to be a subclass of list, all you have to do is "class
      K(list):".

      Comment

      • Peter Otten

        #4
        Re: Multiple inheritance : waht does this error mean ?

        jnair@ensim.com wrote:
        [color=blue]
        > I am using Python 2.4.3
        >[color=green][color=darkred]
        >>>>class K(object,list):[/color][/color]
        > ...: pass
        > ...:
        > ------------------------------------------------------------
        > Traceback (most recent call last):
        > File "<console>" , line 1, in ?
        > TypeError: Error when calling the metaclass bases
        > Cannot create a consistent method resolution
        > order (MRO) for bases object, list[/color]

        class K(object, list): pass

        specifies that a method should be looked up first in object, then in list.
        As list inherits from object, its method resolution order
        [color=blue][color=green][color=darkred]
        >>> list.__mro__[/color][/color][/color]
        (<type 'list'>, <type 'object'>)

        specifies that a method should be looked up first in list, then in object.
        Python refuses to resolve the conflict for you, but you can do it manually:
        [color=blue][color=green][color=darkred]
        >>> class K2(list, object): pass[/color][/color][/color]
        ....[color=blue][color=green][color=darkred]
        >>> K2.__mro__[/color][/color][/color]
        (<class '__main__.K2'>, <type 'list'>, <type 'object'>)

        Now K2, list, and object can agree to look for a method in K2, then list,
        then object. However, you get the same effect by just inheriting from list:
        [color=blue][color=green][color=darkred]
        >>> class K3(list): pass[/color][/color][/color]
        ....[color=blue][color=green][color=darkred]
        >>> K3.__mro__[/color][/color][/color]
        (<class '__main__.K3'>, <type 'list'>, <type 'object'>)

        Peter

        Comment

        • Michele Simionato

          #5
          Re: Multiple inheritance : waht does this error mean ?

          jnair@ensim.com wrote:[color=blue]
          > I am using Python 2.4.3
          >[color=green][color=darkred]
          > >>>class K(object,list):[/color][/color]
          > ...: pass
          > ...:
          > ------------------------------------------------------------
          > Traceback (most recent call last):
          > File "<console>" , line 1, in ?
          > TypeError: Error when calling the metaclass bases
          > Cannot create a consistent method resolution
          > order (MRO) for bases object, list[/color]

          It is explained here:



          Michele Simionato

          Comment

          Working...