[Python-ideas] mixins as decorators vs inheritance [was: Automatic comparisons by default]

Mike Graham mikegraham at gmail.com
Fri Mar 18 20:35:48 CET 2011


On Fri, Mar 18, 2011 at 1:04 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
> On Mar 18, 2011, at 6:58 AM, Mike Graham wrote:
>>
>> (a) is a misfeature. isinstance checks are bad: they make your code
>> less flexible – in Python the actual type of an object isn't something
>> we treat as semantic data. This is *especially* the case with mixins,
>> which are just a convenient thing for code reuse, not some meaningful
>> type.
>
> I believe that is outdated advice.  Since Guido introduced
> abstract base classes, the trend (and grain) of Python is to
> use isinstance() to check for a given interface (i.e. distinguishing
> a Sequence from a Mapping) and to use ABCs as mixins
> (i.e. MutableMapping has replaced UserDict.DictMixin).

Having an object and not knowing whether it's a sequence or a mapping
isn't a good situation to be in and can virtually always be avoided
for the best; having a somewhat saner, more flexible way to check
doesn't really change this. I've yet really to see the added value in
ABCs (other than the stdlib ABCs having useful mixin methods). We were
assured when abcs were introduced that duck typing wasn't dead and
that you don't have to use them, so that's exactly what I've done.

I recognize that a lot of people smarter and more important to Python
than I am decided ABCs were worth having, but I've yet to see the
utility in doing typechecks myself and have a hard time imagining
updating my advice to encourage using isinstance.



More information about the Python-ideas mailing list