Overriding methods inherited from a superclass with methods from a mixin

alanqueiros at gmail.com alanqueiros at gmail.com
Mon Jun 13 19:44:18 EDT 2016


On Monday, June 13, 2016 at 7:29:05 PM UTC-3, Gregory Ewing wrote:
> > I see that in most cases the order doesn't matter, but still I would
> > think that since the correct order is from right to left, that should be the
> > common practice.
> 
> This order is only "correct" if overriding is what you want.
> That's not always going to be the case. The mixin might be
> intended to supply default functionality that can be
> overridden by the classes it's being mixed into. You
> can't say that one order is more correct than the other
> in general.

Yes, but (I presume) ordering it rtl doesn't have any caveat, and the code gets 'future-proof' in case you want to override a method in a class that's intended to be used as a mixin. I for instance am making an effort to get used to the rtl order as quickly as possible. But sure, in most cases it's probably never going to make a difference.
 
>  > Basically, I
> > think of the mixins like plugins of kinds, I'm "adding" functionality to that
> > "base" class I'm inheriting from. Having them to the left sounds like a
> > sandwich recipe that tells you to "slice the ham, put mayonnaise on it, and
> > put it on the bread".
> 
> To me it's more like adding seasoning to a dish. Normally
> you put the main ingredient in first, then add the salt
> and pepper. This is probably the way most people are
> thinking when they write the mixins after the main base
> class.

Exactly.

----

On Monday, June 13, 2016 at 7:51:14 PM UTC-3, Michael Selik wrote:
> > Thank you for your replies. I don't know if I'm quoting you correctly, I'm
> > quite confused with Google Groups... not sure if it's a "forum", something
> > like a mailing list, or both... or neither.
> >
> 
> Mailing list. A "forum" in the metaphorical sense, not the sense of phpBB.
> You're doing fine with quoting, though it would help to add who said what
> when quoting multiple people.
> 
> > since the correct order is from right to left,
> 
> 
> Did you mean left-to-right?
> Are you thinking of Java where there's only one parent class allowed and
> you specify any extra interfaces you're implementing afterwards? Because
> that's not what your code is doing.

Nope, I'm certainly not thinking of interfaces of abstract classes, I'm literally thinking about mixins.

> > Still, pretty much *no one* uses that order [mixin1, mixin2, base]. A quick
> > Google search returns (at least in my "bubble") many blog articles from
> > "notable" Python users with that order wrong.
> >
> 
> Do you mind providing links? I haven't seen anyone "notable" make this
> mistake.

Not at all. Of course the snippets work because they're not overriding, but they all use an 'ltr' order. I feel somewhat bad about posting these links because I don't want to imply the code is wrong or something, since apparently there's no convention on that. So I'm publishing this list with all due respect to the programmers, and this only proves that it's probably more intuitive for most people to put is as [base,mixin]:

class Request(BaseRequest, AcceptMixin, ETagRequestMixin, UserAgentMixin, AuthorizationMixin):
http://stackoverflow.com/a/547714

class RealTestCase(BaseTestCase, MyMixin):
http://nedbatchelder.com/blog/201210/multiple_inheritance_is_hard.html

class TextBook(Book, IndexMixin):
https://ahal.ca/blog/2014/when-would-you-use-python-mixin/

> > > To me it makes sense. English reads left-to-right, so method
> >
> > lookups go left-to-right (and children before parents) in the inheritance
> > > list.
> >
> > Basically I wasn't very confident the code was OK because my intuition
> > said the right way to order the classes I was inheriting from was from left
> > to right.
> 
> That is correct! Write them in the order you are prioritizing the
> definitions. Overrides before bases, left to right.

I'm sorry but it doesn't seem that obvious to me... It feels like building a house starting from the roof to me. Of course that's entirely subjective.

> > I don't see any sense in that analogy you made... sure English and most
> > occidental languages read ltr, but I fail to see how it makes more sense to
> > have the base class on the right and the mixins on the left. Basically, I
> > think of the mixins like plugins of kinds, I'm "adding" functionality to
> > that "base" class I'm inheriting from.
> 
> 
> That's not a good metaphor for what's actually happening. For more detail,
> check out Raymond's "super considered super" video (
> https://www.youtube.com/watch?v=EiOglTERPEo).

I'm going to take a look. Thank you for the link.

> > Don't take my word for it, Google multi inheritance or mixin in Python and
> > let me know if you find someone who thinks it's "natural" to do it that way.
> 
> 
> This might be a bit of selection bias. People who think it makes sense
> might not have an incentive to write a blog post about it. People who don't
> like it will thus be more vocal.

Yes, or maybe it's just my Google results that are biased.

> Ian Lewis seems to be ranked highly by Google (
> https://www.ianlewis.org/en/mixins-and-python).
> I disagree with his assertion that "most people" read an inheritance
> hierarchy top-down with the bases at the left. I think of inheritance as a
> tree. Even if it is a diamond at some point, a depth-first-search of a tree
> isn't far off from the truth. The root (base) is at the bottom, in my mind,
> and the leaves are at the top.

Or maybe you're just very used to thinking like that :). I do understand your logic, but it's not something that happen "intuitively" for me.

Thank you for your replies.



More information about the Python-list mailing list