5

I have a mix-in class called WithAutoNumbering for classes that need a special numbering of a given attribute. Appart from that I have a nice class mix-in called WithIndexing for those classes that need indexing capabilities... which needs the capabilities of WithAutoNumbering.

Some classes need numbering but not indexing, so mixing them together is not a good idea.

The dilemma is, should WithIndexing inherit from WithAutoNumbering? or each class that needs WithIndexing should also inherit from WithAutoNumbering as well?

I.e. this, with CoolClass being the one that has to implement indexing:

class WithAutoNumbering(object):
    ...

class WithIndexing(WithAutoNumbering):
    ...

class CoolClass(WithIndexing):
    ...

or this

class WithAutoNumbering(object):
    ...

class WithIndexing(object):
    ...

class CoolClass(WithIndexing, WithAutoNumbering):
    ...

On the one hand, the first approach is more succint, and makes sure that you can't try to use WithIndexing withouth WithAutoNumbering. On the other hand, I have always read (and found it agreeable) that mix-ins should not have hierarchy, i.e. inherit only from object, in order to avoid spaghettization of the whole class hierarchy with ununderstandable __mro__s

7
  • 1
    This question may fit better at programmers.stackexchange.com Commented Jul 31, 2014 at 15:34
  • @ColonelThirtyTwo Certainly not. This is a technical question. Commented Jul 31, 2014 at 15:54
  • @Marcin Really? It seems like the OP is asking "which is the better design choice?" Isn't that one of the main topics of programmers.se? Commented Jul 31, 2014 at 15:57
  • @ColonelThirtyTwo No, they're asking how to design this in python. Applying the religious concepts of OO design will seriously mislead. Commented Jul 31, 2014 at 15:58
  • @Marcin Surely the question could apply to any other language with mixins? It doesn't seem python specific. Commented Jul 31, 2014 at 16:04

2 Answers 2

1

The issue with making a choice about what your mixins inherit from is that your choice will affect the final MRO of classes which use those mixins.

should WithIndexing inherit from WithAutoNumbering

As you say, WithIndexing uses WithAutoNumbering. It's a funny kind of class which cannot be used on its own at all; and by inheriting from WithAutoNumbering, WithIndexing can override WithAutoNumbering members.

In general, it will be easier for each level to override methods found above, the more you set this up as single inheritance.

However, if you choose to design the other way (such that you need to inherit from both to make a class capable of using WithIndexing), you will likely be completely fine, until something overrides a method found in WithAutoNumbering. In that case, the order in which the classes appear in the base list may affect their order of the MRO, in which case you may have surprising results. If you need the power to affect the MRO in that way, you should have each of these mixins inherit from object. You probably don't need that, though.

Sign up to request clarification or add additional context in comments.

Comments

0

The Liskov substitution principle applies. Would a class designed to work with WithAutoNumbering work with WithIndexing instead (whether or not it actually uses or needs whatever WithIndexing adds)? If not, then WithIndexing should just extend object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.