Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105554
| X-Received | by 10.140.19.4 with SMTP id 4mr1917146qgg.29.1458745413223; Wed, 23 Mar 2016 08:03:33 -0700 (PDT) |
|---|---|
| X-Received | by 10.50.98.70 with SMTP id eg6mr494840igb.4.1458745413189; Wed, 23 Mar 2016 08:03:33 -0700 (PDT) |
| Path | csiph.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!w104no8313401qge.1!news-out.google.com!b22ni1380qge.1!nntp.google.com!w104no8313399qge.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Wed, 23 Mar 2016 08:03:32 -0700 (PDT) |
| In-Reply-To | <mailman.40.1458723109.2244.python-list@python.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=216.252.71.194; posting-account=HCRaUQoAAADmSyZVOH3X2LyMGTp2ojd6 |
| NNTP-Posting-Host | 216.252.71.194 |
| References | <CAFWQgO=o-rPavakLZZBRxAthM5g7xE=Mn4hwcFPxTUvCEXn7SQ@mail.gmail.com> <mailman.40.1458723109.2244.python-list@python.org> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <c97a79ec-9ff2-4bf6-9a09-73ad811d7f5e@googlegroups.com> (permalink) |
| Subject | Re: 0 equals False, was Re: (unknown) |
| From | gst <g.starck@gmail.com> |
| Injection-Date | Wed, 23 Mar 2016 15:03:33 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| X-Received-Bytes | 2547 |
| X-Received-Body-CRC | 2672996004 |
| Xref | csiph.com comp.lang.python:105554 |
Show key headers only | View raw
Le mercredi 23 mars 2016 04:52:02 UTC-4, Peter Otten a écrit :
> 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).
> > 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...
I would include the type in the dictionary key:
d = {}
x = True
d[(type(x), x)] = 42
x = 1
d[(type(x), x)] = "foo"
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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