Skip to content

Instantly share code, notes, and snippets.

@vlazzle
Created January 22, 2010 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlazzle/283369 to your computer and use it in GitHub Desktop.
Save vlazzle/283369 to your computer and use it in GitHub Desktop.
def really_in(y, xs):
if y is not True and y is not False:
'''thanks http://www.reddit.com/r/Python/comments/asmwr/python_boolean_coercion_gotchas/c0j7cd7?context=3'''
return y in xs
for x in xs:
if y is x: return True
return False
True in [1, 2, 3] # -> True
really_in(True, [1, 2, 3]) # -> False
really_in(True, [1, 2, True, 3]) # -> True
False in [0, 6, 7] # -> True
really_in(False, [0, 6, 7]) # -> False
really_in(False, [0, False, 6, 7]) # -> True
really_in(3, [1,2,3]) # -> True
# The downside of using `really_in' instead of `in' is the loss of the ability to
# override `__eq__' to redefine the behavior of `==' and `in'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment