Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105525
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: |
| Date | Wed, 23 Mar 2016 19:43:39 +1100 |
| Lines | 49 |
| Message-ID | <mailman.38.1458722622.2244.python-list@python.org> (permalink) |
| References | <CAFWQgO=o-rPavakLZZBRxAthM5g7xE=Mn4hwcFPxTUvCEXn7SQ@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de scgjMY3V2B291l4HQ0fFewUFxd6oif+/QE3vtA6GgQTg== |
| Return-Path | <rosuav@gmail.com> |
| 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; 'received:209.85.223': 0.03; 'true,': 0.04; 'converts': 0.07; 'ugly': 0.07; 'cc:addr :python-list': 0.09; 'behavior,': 0.09; 'item,': 0.09; 'mess': 0.09; 'overwrite': 0.09; 'python': 0.10; 'wed,': 0.15; "'a',": 0.16; "'b'": 0.16; "(it's": 0.16; '(meaning': 0.16; '2016': 0.16; '23,': 0.16; 'boolean': 0.16; 'compares': 0.16; 'dictionaries': 0.16; 'dictionary.': 0.16; 'different?': 0.16; 'false:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'keys.': 0.16; 'match:': 0.16; 'nick': 0.16; 'pythonic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'with;': 0.16; '{true:': 0.16; 'wrote:': 0.16; 'string': 0.17; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'arguments': 0.22; 'keys': 0.22; 'object.': 0.22; 'defined': 0.23; 'xml': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; '(which': 0.26; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'accepts': 0.29; 'dictionary': 0.29; 'hash': 0.29; 'request,': 0.29; "i'm": 0.30; 'code': 0.30; 'certainly': 0.30; 'probably': 0.31; 'returned': 0.32; 'equal': 0.34; 'advice': 0.35; 'received:google.com': 0.35; 'clear': 0.35; 'false': 0.35; 'something': 0.35; "isn't": 0.35; 'item': 0.35; 'but': 0.36; 'received:209.85': 0.36; '(i.e.': 0.36; 'pm,': 0.36; 'desired': 0.37; 'received:209': 0.38; 'anything': 0.38; 'mean': 0.38; 'why': 0.39; "didn't": 0.39; 'easily': 0.39; 'takes': 0.39; 'some': 0.40; 'easy': 0.60; 'your': 0.60; 'skip:n 10': 0.62; 'different': 0.63; 'mar': 0.65; 'incorporate': 0.66; 'helping': 0.67; 'banging': 0.84; 'blob': 0.84; 'chrisa': 0.84; 'subject::': 0.85; 'to:none': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=ETuOYH37/4G3gaUzlxzXcKEFVkudYfRrHNQkk1UVohk=; b=iXDm3R0OPJBqi2HUA7KIXPRX3BcQbQBepu8Ljw/ny4a8DDc8Cz7nd81a2YWv6XDy/f kOtXCj5jUG0C0kVMX4YOqbylLToSx1nh09t/7MibEjkIAmz3tBVn/vI0MuEn1Uhtzziq v2eC7HjGANhu1+fgzDjJmhvSQd7IzJCkWUrdj5o18fhpfNUc7JVF6Vs36Tfh+FuD7YAQ Hg5iSj4lJWyhM19RY4d5ndQWVFveDYf4HH+NpW+PDMCgXrzivP1i1km/QnjxtVvbCeB8 l/2QoOAevWi/jR9TTh7TFiKCz/Q5i3V0YWSTYr/a2YOZBlMnEo7EDcwszBkpLhOzwJv9 JTIg== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=ETuOYH37/4G3gaUzlxzXcKEFVkudYfRrHNQkk1UVohk=; b=nJhavu70cGsf+pO5Nf8F75t0b3padpwmwtzND1+kcZv6+al/Z31SuNE6l3tR6D9OO4 +pldw9PDhEAFKey51cByD83NcgufsN0SCrJG1+C1oyiQizu0snMLJABrTD0sy8ylskcP z8pODSJHO9tw+qcmXRBHDlXlOPvdbeNLByzxf7U1WoY4vN5pUwXjDAuQGZknsX/TpePk 6eaf6hQflAtFBxheYNUmjw2ApN2/tShxUTjYNkpZmA6+uTnisESO57vpv2icZQkTkY8b q+Laam1q6gAxgpAWfrfC5lN4BZN7YEdPj0jxwaU/u7fS8t27uQqyiyvWx6Z5ZOX02bkI oYNA== |
| X-Gm-Message-State | AD7BkJLEw/i08A1nUYw8srt9pPgos9tpU87sT7LrHAPM/a77TRwlafZQOZMicw0nEa9DC8DNPG+KvCNG0+uwhg== |
| X-Received | by 10.107.128.104 with SMTP id b101mr2042225iod.31.1458722619070; Wed, 23 Mar 2016 01:43:39 -0700 (PDT) |
| In-Reply-To | <CAFWQgO=o-rPavakLZZBRxAthM5g7xE=Mn4hwcFPxTUvCEXn7SQ@mail.gmail.com> |
| 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:105525 |
Show key headers only | View raw
On Wed, Mar 23, 2016 at 2:17 PM, Nick Eubank <nickeubank@gmail.com> wrote:
> 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...
(Presumably everywhere that you write "True" you mean the boolean
value True, not the string "True", which would be completely
separate.)
Dictionary keys are defined on the basis of equality, so anything that
compares equal will match:
>>> d = {True: 'a', False: 'b'}
>>> d[0]
'b'
>>> d[0.0]
'b'
Since you have True and 1 meaning very different things, your program
is already somewhat non-Pythonic. So your best solution is probably
going to be something messy, like having your dictionary keys
incorporate the type of the object. This isn't going to be easy or
clean, but if that's what you have to work with, so be it. (It's not
half as bad as the mess I was helping one of my students with; she's
coping with a Python module that's a hyper-thin layer over an HTTP/XML
request, and it simply takes the returned XML blob and naively
converts it into a Python dictionary. Makes for some ugly code - but
that's not her fault.)
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Chris Angelico <rosuav@gmail.com> - 2016-03-23 19:43 +1100
csiph-web