Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 2: True/False literal #108

Closed
mishaturnbull opened this issue Dec 9, 2018 · 4 comments
Closed

Python 2: True/False literal #108

mishaturnbull opened this issue Dec 9, 2018 · 4 comments

Comments

@mishaturnbull
Copy link

In Python 2 (tested 2.7.15+ on Debian), the boolean operators True and False are in fact not literals, and can be reassigned. Unfortunately, I can't remember where I found this, but I've used it a few times to mess with people's code. Good fun :)

>>> True
True
>>> False
False
>>> True == False
False  # as expected!
>>> True = False  # Note, only 1 '='.  This is assignment!
>>> True
False    # !!
>>> True == False  # normally, False.  Now, we're expecting...
True

The reverse is also possible:

>>> False = True  # assignment, not equality check
>>> if False: print("Hello World!")    # this should never work.
...
Hello World!

Unfortunately, this behavior was removed in Python 3 by setting True and False to be literals. I can't seem to find this alluded to in the list; let me know if it's worthy of a PR.

@satwikkansal
Copy link
Owner

@mishaturnbull Yes, it's definitely worth a PR. We already have a few WTFs that only work on specific versions of python, so there is no issue adding more as long as they're interesting.

@mishaturnbull
Copy link
Author

Awesome! This week is finals for me, but I should be able to get to it post-Wednesday.

@satwikkansal
Copy link
Owner

No problem, Looking forward to it!

@mishaturnbull
Copy link
Author

Finally got to the PR, and... found the example already in the list! I must've missed it the first time around.

https://github.com/satwikkansal/wtfpython#-when-true-is-actually-false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants