Test 0 and false since false is 0

Rhodri James rhodri at kynesim.co.uk
Tue Jul 11 10:05:18 EDT 2017


On 11/07/17 14:16, Albert-Jan Roskam wrote:
> From: Python-list <python-list-bounces+sjeik_appie=hotmail.com at python.org> on behalf of Dan Sommers <dan at tombstonezero.net>
> Sent: Friday, July 7, 2017 2:46 AM
> To: python-list at python.org
> Subject: Re: Test 0 and false since false is 0
>      
> On Thu, 06 Jul 2017 19:29:00 -0700, Sayth Renshaw wrote:
> 
>> I have tried or conditions of v == False etc but then the 0's being
>> false also aren't moved. How can you check this at once?
> 
> Maybe this will help:
> 
>      Python 3.5.3+ (default, Jun  7 2017, 23:23:48)
>      [GCC 6.3.0 20170516] on linux
>      Type "help", "copyright", "credits" or "license" for more information.
>      >>> False == 0
>      True
>      >>> False is 0
>      False
> 
> 
> =====> Just wondering: Is this 'is' test depending on an implementation detail of cPython (small ints, I forgot how small 0-255 maybe, are  singletons)?

No, it's not an implementation detail.  True and False are singletons, 
and are different objects from 1 and 0 (they behave differently when 
converted to strings, for example).  Now, relying on "0 is 0" being true 
would be relying on an implementation detail, but you weren't going to 
do that, were you?

-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list