[Python-ideas] __iter__ implies __contains__?

Nick Coghlan ncoghlan at gmail.com
Sun Oct 2 16:43:40 CEST 2011


On Sun, Oct 2, 2011 at 7:21 AM, Masklinn <masklinn at masklinn.net> wrote:
>
> On 2011-10-02, at 03:01 , Nick Coghlan wrote:
>
>> On Sat, Oct 1, 2011 at 4:24 PM, Chris Rebert <pyideas at rebertia.com> wrote:
>>> Requiring the explicit marking of a class as a sequence by inheriting
>>> from the Sequence ABC in order to get such default behavior "for free"
>>> seems quite reasonable. And having containment defined by default on
>>> potentially-infinite iterators seems unwise. +1 on the suggested
>>> removals.
>>
>> -1 to any removals - fallback protocols are the heart of duck-typing
> I very much disagree with this assertion. In fact I'd make the opposite
> one: fallback protocols are a perversion of duck-typing and only serve
> to make it less reliable and less predictable.
>
> In keeping with ducks, you were looking for one, didn't find anything
> which quacked or looked like a duck. The fallback protocol kicks in,
> you get something with feathers which you found near water and went
> "good enough". Now you drop it from 10000m because you're looking into
> the efficiency of a duck's flight starting airborne, and observe your
> quite dismayed penguin barreling towards the ground.
>
> A few seconds later, you find yourself not with additional experimental
> data but with a small indentation in the earth and a big mess all over
> it.

I love that imagery :)

However, it's the kind of situation that's part and parcel of duck
typing - you try things and see if they work and the occasional
penguin gets it in the neck. If that's inadequate for a given use
case, you define an ABC and register only things you've already
checked and found to behave correctly (although beware if you register
Bird rather than FlyingBird - the penguins, emus and friends may still
be in trouble at that point)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list