[Python-ideas] True and False are singletons

Chris Angelico rosuav at gmail.com
Mon Mar 18 16:59:56 EDT 2019


On Tue, Mar 19, 2019 at 7:53 AM Wes Turner <wes.turner at gmail.com> wrote:
>
> 'True' is a keyword. (Which is now immutable in Python 3.X?)
>
> >>> True = 1
>   File "<stdin>", line 1
> SyntaxError: can't assign to keyword

In Python 3, the source code token "True" is a keyword literal that
always represents the bool value True.

> In Python 2:
>
> >>> True
> True
> >>> True is True
> True
> >>> True = 1
> >>> True is 1
> True
> >>> True is None
> False
> >>> True = None
> >>> True is None
> True

In Python 2, the source code token "True" is simply a name, and there
is a built-in of that name. Before it became a built-in, it was common
for scripts to have their own definitions of True and False [1], so to
avoid unnecessary breakage, they were made assignable in the normal
way. Python 3 simplifies this by making them keywords.

But either way, the *values* True and False are special, and are the
only two instances of the bool type that will ever exist.

ChrisA

[1] Note that I learned about this in history class; it was before my time.


More information about the Python-ideas mailing list