How to order base classes?

K

kj

Suppose that I want to write a subclass C of base classes A and B.
What considerations should go into choosing the ordering of A and
B in C's base class list?

Since any order one chooses can be overridden on a per-method basis,
by assigning the desired parent's method to the appropriate class
attribute, like this:

class C(B, A)
# override methods spam, ham, and eggs from B
spam = A.spam;
ham = A.ham;
eggs = A.eggs;
...

....it is difficult for me to see a strong compelling reason for picking
an ordering over another. But may be just ignorance on my part.

How should one go about deciding the ordering of base classes?

TIA!

~kj
 
S

Steven D'Aprano

How should one go about deciding the ordering of base classes?

There is no general way for doing so. You need to consider the actual
functionality of the methods involved. Consider a method spam() of class
C that inherits from both A and B. To be completely general, you might
have any of the following situations:

C.spam() overloads A.spam() followed by B.spam()
C.spam() overloads B.spam() followed by A.spam()
C.spam() overloads A.spam() and overrides B.spam()
C.spam() overloads B.spam() and overrides A.spam()
C.spam() overrides both A.spam() and B.spam()

(where I use "overload" to mean "modify the behaviour of", and "override"
to mean "change the behaviour completely" -- basically, overloading will
call the superclass' method, while overriding will not.)

And (again, we're being completely general) whatever choice you make for
C.spam() may not be valid for C.ham(), which could behave completely
differently.


The question you ask can only be answered in reference to a specific
class with specific methods. There is no general principle, it depends
entirely on the problem being solved.
 
K

kj

In said:
The question you ask can only be answered in reference to a specific
class with specific methods. There is no general principle, it depends
entirely on the problem being solved.

Thanks!

~kj
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top