Test 0 and false since false is 0
Grant Edwards
grant.b.edwards at gmail.com
Tue Jul 11 10:11:36 EDT 2017
On 2017-07-11, Albert-Jan Roskam <sjeik_appie at hotmail.com> 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.
False is required to be a singleton.
Therefore, if you want to know if <whatever> is the boolean object
False, you can use '<whatever> is False' with predictable results..
Integer values are not required to be singletons, so you cannot depend
on the value of <whatever> is 0, or <whatever> is 12345678. As you
mention, in the current version(s) of CPython, small integer values
are cached, but larger ones are not:
$ python
Python 2.7.12 (default, Jan 3 2017, 10:08:10)
[GCC 4.9.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 0
>>> x is 0
True
>>> x = 12345678
>>> x is 12345678
False
>>>
That could change tomorrow.
--
Grant Edwards grant.b.edwards Yow! PEGGY FLEMMING is
at stealing BASKET BALLS to
gmail.com feed the babies in VERMONT.
More information about the Python-list
mailing list