Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105527

0 equals False, was Re: (unknown)

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject 0 equals False, was Re: (unknown)
Date Wed, 23 Mar 2016 09:51:34 +0100
Organization None
Lines 51
Message-ID <mailman.40.1458723109.2244.python-list@python.org> (permalink)
References <CAFWQgO=o-rPavakLZZBRxAthM5g7xE=Mn4hwcFPxTUvCEXn7SQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de EVk7Q2KLzTQ6yBlpl2Xzlwr9RBvHJbrultRRnlj1X68Q==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'true,': 0.04; 'context': 0.05; 'false,': 0.07; 'feature.': 0.07; 'behavior,': 0.09; 'bool': 0.09; 'discriminate': 0.09; 'item,': 0.09; 'overwrite': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'bug': 0.10; 'python': 0.10; "'a',": 0.16; '(meaning': 0.16; '0.0,': 0.16; 'dictionaries': 0.16; 'different?': 0.16; 'false:': 0.16; 'fits': 0.16; 'keys.': 0.16; 'nick': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; '{true:': 0.16; 'wrote:': 0.16; 'case.': 0.18; 'odd': 0.18; 'all,': 0.20; 'versions': 0.20; 'arguments': 0.22; 'header:User-Agent:1': 0.26; '(which': 0.26; 'header:X-Complaints-To:1': 0.26; 'compatible': 0.27; 'function': 0.28; 'accepts': 0.29; 'dictionary': 0.29; 'hash': 0.29; "i'm": 0.30; 'certainly': 0.30; "i'd": 0.31; 'true.': 0.33; 'advice': 0.35; 'clear': 0.35; 'false': 0.35; 'identity': 0.35; 'item': 0.35; 'but': 0.36; '(i.e.': 0.36; 'to:addr:python-list': 0.36; 'received:org': 0.37; 'desired': 0.37; 'why': 0.39; 'sure': 0.39; "didn't": 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'behavior': 0.61; 'provide': 0.61; 'today,': 0.62; 'different': 0.63; 'between': 0.65; 'banging': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd95b9.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:105527

Show key headers only | View raw


Nick Eubank wrote:

> Hello All,
> 
> 
> Found an odd behavior I'd never known about today, not sure if it's a bug
> or known. Python 3.4.4 (anaconda).

This is a feature. Old versions of Python did not have True and False, so 
they were added in a compatible way.

> True, False, 0, 1 can all be used as dictionary keys.
> 
> But Apparently True and 1 hash to the same item and False and 0 hash to
> the same item, so they can easily overwrite (which I spent a while banging
> my head over today).
> 
> In other words:
> 
>  In[1]:
>      d = {True: 'a', False: 'b'}
>      d[0] = 'z'
>      d[False]
> 
> Out[1]:
>      'z'
> 
> I understand that True and False are sub-types of ints, but it's not clear
> to me why (i.e. certainly didn't feel intuitive) that they would be
> treated the same as keys.
> 
> Relatedly, if this is a desired behavior, any advice one how best to work
> with dictionaries when one wants "True" and 1 to be different? I'm working
> on a function that accepts arguments that may be "True" or 1 (meaning very
> different things) and am seeking a pythonic solution...

The pythonic solution is "don't do this". The == operator cannot 
discriminate between 0, 0.0, and False, or 1, 1.0, and True. 

True and False are singletons, so you can check identity with

x is True or x is False

A type check will also work: 

type(x) == bool
isinstance(x, bool) # bool cannot be subclassed

If you provide some context we may be able to come up with an alternative 
approach that fits your use case.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

0 equals False, was Re: (unknown) Peter Otten <__peter__@web.de> - 2016-03-23 09:51 +0100
  Re: 0 equals False, was Re: (unknown) gst <g.starck@gmail.com> - 2016-03-23 08:03 -0700

csiph-web