Created
January 22, 2010 00:38
-
-
Save vlazzle/283369 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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