Is there a reason that True and False can still be changed in Python 2?

Built-in constants Trueand Falseare unmodifiable in Python 3 , since at least 3.3:

False value of type bool. Assignments Falseare illegal and pick up SyntaxError.

True value of type bool. Assignments Trueare illegal and pick up SyntaxError.

However, in python 2.7, code True = 0is executed without any exceptions. It seems that these restrictions should have been moved to 2.x a long time ago, but they did not.

The only reason I can think of is compatibility with the existing code base. Do you know any case where existing software intentionally modifies Trueor False? If not, what other reasons could there be?

+4
source share
2 answers

As Guido states in the story None, TrueandFalse , the context matters if not accepted Trueand the Falsekeywords. When bool was introduced (with PEP 285 in Python 2.3), it could not be made a keyword, because a lot of existing code already defined it anyway:

True/False . , . true false, True False, TRUE FALSE, . , , True False , , True False .

:

, , , True False , . , True False ( ), , Python 3, , True False None.

, :

, , - .

, . Python 2 , True False .

Python 3 , , , , True False .

+6

, True False , True False , , builtin. True False , , , :

>>> True = 'mumble'
>>> True
'mumble'
>>> bool(True)
True
>>> bool(True) == True # "mumble" == True
False
>>> bool(True) == bool(True)
True

, , .

+2

Source: https://habr.com/ru/post/1685910/


All Articles