Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1037453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:51:13+00:00 2026-05-16T14:51:13+00:00

A very simple case of diamond-type inheritance: class Root: def f(self): print(‘Root’) class A(Root):

  • 0

A very simple case of diamond-type inheritance:

class Root:
    def f(self):
        print('Root')

class A(Root): pass

class B(Root):
    def f(self):
        print('B')

class AB(A, B): pass

AB().f()

According to Python 3.1.2 documentation:

For most purposes, in the simplest
cases, you can think of the search for
attributes inherited from a parent
class as depth-first, left-to-right,
not searching twice in the same class
where there is an overlap in the
hierarchy.

Hence, I expect my example to resolve in the order: AB -> A -> Root -> B. But it doesn’t. Using ActiveState Python 3.1.2, the output is ‘B’ rather than ‘Root’.

What am I missing?

Also, I noticed that ActiveState Python 2.6 prints ‘Root’ with the same code. Did the resolution rules change between 2.6 and 3.1?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:51:14+00:00Added an answer on May 16, 2026 at 2:51 pm

    Python 2.6 printed Root because Root is an old-style class. It should print B if Root inherits from object (making it a new-style class).

    Old style class doesn’t use the C3 MRO, but the old flawed MRO, which is exactly what your quoted text describes.


    The reason B appears before Root is because B inherits from Root also.

    The inheritance graph of AB is:

     AB - A - Root -- object
        \   /
          B
    

    So:

    mro(object) = [object]
    mro(Root)   = [Root] + mro(object)   # mro of single-inherited class is simple...
                = [Root, object]
    mro(A)      = [A, Root, object]
    mro(B)      = [B, Root, object]
    

    in case of multiple inheritance, the MRO is computed by taking the elements in the superclass’s MROs from left to right, that doesn’t appear in the middle. Better explained in example:

    mro(AB) = [AB] + merge(mro(A), mro(B))
            = [AB] + merge([A, Root, object], [B, Root, object])   
            = [AB] + [A] + merge([Root, object], [B, Root, object])
               # A appears at head, pop it.
            = [AB] + [A] + [B] + merge([Root, object], [Root, object])
               # cannot pop Root because Root is not at head of [B, Root, object]
               # pop B instead.
            = [AB] + [A] + [B] + [Root] + merge([object], [object])
               # now we can pop Root because it only appears at the head position
            = [AB] + [A] + [B] + [Root] + [object]
            = [AB, A, B, Root, object] 
    

    Further detail can be found in http://www.python.org/download/releases/2.3/mro/.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take a very simple case as an example, say I have this URL: http://www.example.com/65167.html
I have recorded a very simple test case Using the Selenium IDE integrated with
My use case is very simple : I have a GUI application, and inside
I have a very simple case that I think would benefit from using templates
This should be a very simple case, but I am pulling hair trying to
Very simple question, is there any cloud server enviroments avaliable these days for us
This very simple code gives me tons of errors: #include <iostream> #include <string> int
A very simple ?: operator in C#, combined with a simple array and LINQ
I have a very simple problem which requires a very quick and simple solution
Below I have a very simple example of what I'm trying to do. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.