Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is vs in
#1
i am doing testing for exactly the same as opposed to equivalent. this means using the is keyword for comparison. comparing 1 to True (for example) i want to be False. but i have cases in a new project where i need to test if a variable is (or is not) the same as any of a few values. the in keyword does not compare like is. it compares more like ==. is there a way to do an is-like comparison against a list (or tuple) of values? i tried this in the hope there was a way but that is not implemented.
if this is in that:
    print('yes')
else:
    print('no')
if this cannot be done, then that syntax is my suggested addition.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
No way to redefine it that I'm aware of.

Quote:For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y is equivalent to any(x is e or x == e for e in y).

Given that, I would just redo your test as the same comprehension, just without the equality test.

>>> item = 1
>>> mylist = [True]
>>> item in mylist
True
>>> any(x is item for x in mylist)
False
Skaperen likes this post
Reply
#3
(Dec-20-2020, 03:00 AM)bowlofred Wrote: Given that, I would just redo your test as the same comprehension, just without the equality test.

the appears to be the solution.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020