Skip to content
Advertisement

Why is “Y” printed instead of “X” in this MRO?

Could somebody explain why this code prints Y and not X? I expected it to print “X” because it says pass in Class C and Class X is the next super class.

JavaScript

Sorry for the long code, but I dont know how to make it shorter without making the question unclear

Advertisement

Answer

Per Python Multiple Inheritance: The Diamond Rule:

  1. List all the base classes, following the classic lookup rule and include a class multiple times if it’s visited repeatedly. In the above example, the list of visited classes is [D, B, A, C, A].
  2. Scan the list for duplicated classes. If any are found, remove all but one occurrence, leaving the last one in the list. In the above example, the list becomes [D, B, C, A] after dropping duplicates.
JavaScript

Y is printed because Y class is listed first on the mro order.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement