Setting the value of True

J

jeangawron

Python allows you set the value of True

Now this is consistent with the decision to let you set the
value of various builtin names. But why is this case different:
File "<stdin>", line 1
SyntaxError: cannot assign to None

Mark Gawron
 
G

Gary Herron

Python allows you set the value of True

Now this is consistent with the decision to let you set the
value of various builtin names. But why is this case different:

File "<stdin>", line 1
SyntaxError: cannot assign to None

Mark Gawron

Python3 fixes this inconsistency, by disallowing all such assignments.

They all raise an exception:
SyntaxError: assignment to keyword
 
T

Terry Reedy

Python allows you set the value of True

Unqualified 'Python', used in the present tense, refers to the latest
release or repository version.
SyntaxError: assignment to keyword
 
S

Steven D'Aprano

Python allows you set the value of True

Only in older versions of Python. This is for historical reasons: True
and False were added as built-ins relatively late in Python's history
(2.2, I think) and so there is still old code that legitimately does this
at the start of a module:

True = 1
False = not True

Hence, making True and False actual keywords could only occur in Python
3, which was allowed to break backwards compatibility.

None, on the other hand, has always existed in Python. The very oldest
versions way back in the mists of time allowed you to rebind None:

[steve@ando ~]$ python1.5
Python 1.5.2 (#1, Aug 27 2012, 09:09:18) [GCC 4.1.2 20080704 (Red Hat
4.1.2-52)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam3


but as that was considered a mistake, *and* it was considered unlikely to
be any code in the wild that legitimately rebinds None, that was made a
SyntaxError in Python 2.x.

As for why None, True and False are treated differently than built-ins,
if I remember the reason why, it is because they are considered
fundamental to the inner workings of Python, unlike mere builtins like
len, map, etc. and therefore should be protected.
 
G

Gregory Ewing

Steven said:
As for why None, True and False are treated differently than built-ins,
if I remember the reason why, it is because they are considered
fundamental to the inner workings of Python, unlike mere builtins like
len, map, etc. and therefore should be protected.

It's probably more to permit optimisation. Treating them
as constants avoids a name lookup every time they're used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top