[Python-ideas] Non-boolean return from __contains__

Raymond Hettinger raymond.hettinger at gmail.com
Sun Jul 25 20:48:23 CEST 2010

On Jul 25, 2010, at 11:15 AM, Alex Gaynor wrote:

> Recently I've been wondering why __contains__ casts all of it's
> returns to be boolean values.  Specifically I'd like to propose that
> __contains__'s return values be passed directly back as the result of
> the `in` operation.

x = y in z          # where x is a non boolean.


One of the beautiful aspects of __contains__ is that its simply signature
allows it to be used polymorphically throughout the whole language.
It would be ashamed to throw-away this virtue so that you can
have a operator version of something that should really be a method
(like find() for example).

-1 on the proposal because it makes the language harder to grok
while conferring only a dubious benefit (replacing well named
methods with a non-descriptive use of an operator).

There is no "natural" interpretation of an in-operator returning
a non-boolean.  If the above snippet assigns "foo" to x, what
does that mean?  If it assigns -10, what does that mean?
Language design is about associating meanings (semantics)
with syntax.  ISTM, this would be poor design.


More information about the Python-ideas mailing list