Unexpected behaviour from the 'in' operator

Andrew Bennetts andrew-pythonlist at puzzling.org
Mon Feb 24 00:49:36 EST 2003


On Mon, Feb 24, 2003 at 05:19:31PM +1300, Blair Hall wrote:
[..snip..]
> x and y are not the same object!  It seems to me that
> the comparison should use something like the id() function
> instead of __eq__.

I've sometimes thought that an "is in" operator would be useful, so you
could test if something contains the exact object, rather than just
something equal to an object.  

It's pretty rare that I need it, though; mostly I'd think to use it for a
slight speed-up, which is almost certainly wasted effort.  On the other
hand, it could be genuinely useful if the objects had very slow equality
operators... I'm not sure it's useful enough to justify extending the
language for though.

Also, having to explain to newbies why "if x is in seq" is distinct from "if
x in seq" is probably not worth the pain.  It's a tempting idea though; it's
such a small addition, and doesn't require any new keywords... maybe I should
write a PEP :)

-Andrew.






More information about the Python-list mailing list