Overriding True and False ?
Irv at furrypants.com
Mon Jan 30 00:03:33 EST 2017
I teach intro to programming using Python. In my first assignment, students are asked to assign variables of different types and print out the values.
One student (who really did not understand Booleans) turned in the following for his/her interpretation of Booleans (Python 2.7):
True = 'shadow'
False = 'light'
print "If the sun is behind a cloud, there is", True
print "If it is a clear day, there is", False
And it printed:
If the sun is behind a cloud, there is shadow
If it is a clear day, there is light
It seems very odd that Python allows you to override the values of True and False. In the code, True and False were clearly recognized as keywords as they were colored purple. But there was no error message.
You cannot assign new values to other keywords. Simple tests of things like:
for = 5
while = 2
not = 3
As expected, all result in SyntaxError: invalid syntax. Why would Python allow you to override the values of True and False? I wonder if this is some sort of historical thing as these are the only keywords besides None that are uppercased. This line:
None = 5
Even gives a special SyntaxError: cannot assign to None
More information about the Python-list