How to order base classes?

Discussion in 'Python' started by kj, Dec 24, 2010.

  1. kj

    kj Guest

    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
    kj, Dec 24, 2010
    #1
    1. Advertising

  2. On Fri, 24 Dec 2010 03:36:28 +0000, kj wrote:

    > 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.


    --
    Steven
    Steven D'Aprano, Dec 24, 2010
    #2
    1. Advertising

  3. kj

    kj Guest

    In <4d14209d$0$30000$c3e8da3$> Steven D'Aprano <> writes:

    >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
    kj, Dec 24, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ignacio Machin \( .NET/ C#  MVP \)

    order of execution of page_load in a base and derived classes

    Ignacio Machin \( .NET/ C# MVP \), Sep 1, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    888
    Ignacio Machin \( .NET/ C# MVP \)
    Sep 2, 2004
  2. Soren Kuula
    Replies:
    2
    Views:
    482
    Soren Kuula
    Feb 1, 2004
  3. Colin Goudie
    Replies:
    6
    Views:
    454
    Victor Bazarov
    Jan 26, 2004
  4. coltrane
    Replies:
    5
    Views:
    464
    James Kanze
    Aug 25, 2010
  5. cspoh
    Replies:
    0
    Views:
    234
    cspoh
    Jul 31, 2003
Loading...

Share This Page