[Python-ideas] Programming recommendations (PEP 8) and boolean values

M.-A. Lemburg mal at egenix.com
Thu Aug 9 10:33:44 CEST 2012


I think you're reading to much into the "is". Sure, "x is True" reads
like "x has the property of being true", but that's not what the
"is" operator is all about. It's about an identity check that works
fine when you want to check for identity, but doesn't otherwise.

For "None" and other singletons, the "is" operator is the perfect
choice, since it avoids programming errors and is a very fast
way to check a value against the singleton.

For non-singletons, "is" rarely makes sense and can indeed introduce
programming errors.

Now, True and False are singletons in their own right, but they are
also special integers 1 and 0, nothing more, nothing less.

So if you are interested in checking whether a function does indeed
use these special integers, you can use the "is" operator, but apart
from testing, where this may sometimes be needed, the much more common
use case is not to check for the special integers, but instead check
for property of the return value, i.e. whether the return value has
the true property or not and you usually apply that check in a different
way:

if x:
    print 'true assumption'
else:
    print 'false assumption'

By checking just for the True singleton, it is well possible that
you'll miss a lot of other true values, e.g. 42 is true, but a
different integer, so "42 is True" will return false.
Likewise, checking for False won't catch the 0.0 float value,
an empty string or an empty list.


INADA Naoki wrote:
> In [1]: True + True
> Out[1]: 2
> 
> I think Python 4 should raise ValueError.
> 
> On Thu, Aug 9, 2012 at 4:35 PM, Yuval Greenfield <ubershmekel at gmail.com> wrote:
>> On Thu, Aug 9, 2012 at 10:15 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
>>>
>>> Le 08/08/2012 16:28, Guido van Rossum a écrit :
>>>
>>>> I'd be strongly against changing that rule. If you don't want other
>>>> types than bool, use an isinstance check. Code using "is True" is most
>>>> likely a bug. (It's different for None, since that has no second value
>>>> that is assumed.)
>>>
>>>
>>> That said, I'm also curious about the answer to Michael's following
>>> question:
>>> “why does it say that using an identity check is worse than an equality
>>> check?”
>>>
>>
>> In python 3.2.3:
>>
>>     >>> 1 == True
>>     True
>>     >>> 13 == True
>>     False
>>     >>> bool(1)
>>     True
>>     >>> bool(13)
>>     True
>>     >>> 1 is True
>>     False
>>     >>> 13 is True
>>     False
>>
>> To my surprise identity is actually less confusing than equality. So I agree
>> with Antoine and Michael on that point.
>>
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
>>
> 
> 
> 

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Aug 09 2012)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2012-08-25: FrOSCon, St. Augustin, Germany ...             16 days to go

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/





More information about the Python-ideas mailing list