Skip to content

Python 2: True/False literal #108

Closed
Closed
@mishaturnbull

Description

@mishaturnbull

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions