[Python-ideas] True and False are singletons

Serhiy Storchaka storchaka at gmail.com
Tue Mar 19 03:56:21 EDT 2019


18.03.19 22:58, Greg Ewing пише:
> Oleg Broytman wrote:
>>    Three-way (tri state) checkbox. You have to distinguish False and
>> None if the possible valuse are None, False and True.
> 
> In that case the conventional way to write it would be
> 
>      if settings[MY_KEY] == True:
>          ...
> 
> It's not a major issue, but I get nervous when I see code
> that assumes True and False are unique, because things
> weren't always that way.

"x == True" looks more dubious to me than "x is True". The latter can be 
intentional (see for example the JSON serializer), the former is likely 
was written by a newbie and contains a bug. For example, 1 and 1.0 will 
pass this test, but 2 and 1.2 will not.

Python 3.8 will emit a syntax warning for "x is 1" but not for "x is 
True", because the latter is well defined and have valid use cases.



More information about the Python-ideas mailing list