On Thursday, March 5, 2015 at 2:27:12 PM UTC-8, rand...@fastmail.us wrote:
> It's been brought up on Stack Overflow that the "in" operator (on
> tuples, and by my testing on dict and list, as well as dict lookup) uses
> object identity as a shortcut, and returns true immediately if the
> object being tested *is* an element of the container. However, the
> contains operation does not specify whether object identity or equality
> is to be used. In effect, the built-in container types use a hybrid
> test: "a is b or a == b".
> 
> My question is, is this a *correct* implementation of the operator, or
> are objects "supposed to" use a basis of equality for these tests?

I would argue that if `a is b` then it is obvious that `a == b`, so if all you 
care about is whether or not `a == b`, then it shouldn't matter if `a is b` is 
checked first.  It could greatly speed up comparisons for objects that are 
expensive to compare.

I would also argue that the "in" operator *SHOULD* be using equality of value.  
Otherwise, if it only used equality of identity, testing if user input is valid 
by seeing if it is "in" a list of valid inputs wouldn't work.  Testing identity 
in this case would *never* be true unless you were dealing with a select number 
of integers.

>>> i = input()
5
>>> i is 5
True
>>> i = intput()
999999
>>> i is 999999
False
>>> i in [1, 2, 3, 999999]
True
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to