[Python-ideas] __contains__=None (was: __iter__ implies __contains__?)

Nick Coghlan ncoghlan at gmail.com
Tue Oct 4 05:34:29 CEST 2011


On Mon, Oct 3, 2011 at 10:35 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
>
> On Oct 3, 2011, at 8:09 PM, Jan Kaliszewski wrote:
>
> Maybe there should be possible to explicitly disallow the 'in' test by
> setting __contains__ to None (similar to already settled __hash__=None
> for non-hashables).
>
> We need fewer arcane rules -- not more of them.

Indeed, __hash__ = None was a special case forced on us by the fact
that object defines __hash__ and the Hashable ABC was implemented to
use a duck typed instance check. The interpreter needed a way to
disable hashing when users defined a custom __eq__ implementation
without overriding __hash__ themselves.

I suspect isinstance(obj, collections.Container) is already an
adequate test to separate out "real" containers from mere iterators,
and ferreting out subtle distinctions like that where duck typing
isn't up to the task is one of the main reasons ABCs were added. (I
earlier indicated I didn't think that was the case yet, but I
subsequently realised that was due to my using isinstance() to check
things at the interactive prompt when I should have been using
issubclass())

Cheers,
Nick.

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



More information about the Python-ideas mailing list