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


Groups > comp.lang.python > #71032

Re: Normal dict behavior?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.023
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'duplicate': 0.07; 'none,': 0.07; 'exist,': 0.09; 'key.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '(key,': 0.16; 'antoine': 0.16; 'declared': 0.16; 'dict': 0.16; 'equal.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ignored,': 0.16; 'internally': 0.16; 'internals,': 0.16; 'key/value': 0.16; 'ought': 0.16; 'sense,': 0.16; 'wrote:': 0.18; 'obviously': 0.18; "python's": 0.19; 'thu,': 0.19; 'value.': 0.19; '>>>': 0.22; 'code,': 0.22; 'handles': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'choices': 0.24; 'integer': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'mention': 0.26; 'second': 0.26; 'defined': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'points': 0.29; 'possibility': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; '>>>>': 0.31; 'values.': 0.31; 'languages': 0.32; 'are:': 0.33; 'used,': 0.33; 'could': 0.34; "can't": 0.35; 'agree': 0.35; 'something': 0.35; 'case,': 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'are,': 0.36; 'representing': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'should': 0.36; 'effort': 0.37; 'error.': 0.37; 'so,': 0.37; 'two': 0.37; 'performance': 0.37; 'mapping': 0.38; 'nov': 0.38; 'that,': 0.38; 'little': 0.38; 'does': 0.39; 'extremely': 0.39; 'sure': 0.39; 'how': 0.40; 'first': 0.61; 'times': 0.62; "you'll": 0.62; 'kind': 0.63; 'choose': 0.64; 'more': 0.64; 'different': 0.65; 'therefore': 0.72; 'carefully': 0.74; 'gain': 0.79; '"spam"': 0.84; 'dict,': 0.84; 'etc,': 0.84; 'intern': 0.84; 'pervasive': 0.84; 'worth?': 0.84; '2013,': 0.91; 'why?': 0.91; 'to:none': 0.92; 'imagine': 0.93
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 :content-type; bh=dG/M/V0Luh32KxLjiZqUa7U2IBxoQ07O5Ua68wAQfcc=; b=XALHyh0Ybple+JG55uOKcUyMikCIL4q3cwH2vv9Bj3ABM8vu0o/lNY8bU3/Lme4l0W BwPtpBzOIzl0wEE4Pu5cjvWkTpZyHzwYrJvqsXR3g72FRISgEOj+gxwZ4G9AJhVnwd+U PJfzFhf1P17PfwflKQhiZjmWjFseBwndahNmxXTI1wcO/kCSxyStrl4s1a6K4Y5fwyoO V4kmsWbHAqXCj4mFNMThconOx7sBLqPFgUl0hTS2Z1qFZKZ/s7Ecco9PttOd3WvHtaui CYxNAUOCNgqGqS8nVyxLrX5Te1k7M26/zISleYssl6wX6MCmzru60vMgaGeu+XNnjZof k9Qw==
MIME-Version 1.0
X-Received by 10.220.133.197 with SMTP id g5mr39458432vct.20.1399476179629; Wed, 07 May 2014 08:22:59 -0700 (PDT)
In-Reply-To <18f61e5d-f47b-4373-a835-67c6f20ef3d1@googlegroups.com>
References <18f61e5d-f47b-4373-a835-67c6f20ef3d1@googlegroups.com>
Date Thu, 8 May 2014 01:22:59 +1000
Subject Re: Normal dict behavior?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.9739.1399476181.18130.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1399476181 news.xs4all.nl 2948 [2001:888:2000:d::a6]:44002
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:71032

Show key headers only | View raw


On Thu, May 8, 2014 at 1:06 AM, antoine <boolegue@gmail.com> wrote:
> Hi,
>
> Python 2.7.5 (default, Nov 20 2013, 14:20:58)
> [GCC 4.7.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> {0.: None, 0:None}
> {0.0: None}
>
> The second item disappeared!
>
> Why?
> Is it normal?

There are two things happening here. Firstly:

>>> 0 == 0.0
True

Secondly:

>>> {"spam":1, "spam":2}
{'spam': 2}

I think you'll agree that, in the second case, Python cannot store
both values. You might say that this ought to be an error, but
certainly it can't return a dict with two values when you attached
them to the same key.

The first part is that a dict is defined on the basis of equality, and
the integer 0 and the floating point 0.0 are equal. So to the dict,
they are just as much equal as the two strings "spam" are, and it's a
duplicate key.

Obviously the dict can't be defined on the basis of object identity,
as you'd then have to carefully intern all strings used, etc, etc.
(Though an identity-based mapping would have some value. i'm sure you
could make a MutableMapping that internally maps id(key) to (key,
value) and handles everything uniquely. Might already exist, even. But
it's definitely not what the inbuilt dict should do.) So the two
arguable points are:

1) Should 0 and 0.0 compare equal? Both choices make sense, and
different languages choose differently, but Python has declared that
numerics representing the same number are equal. So, this one isn't
changing.

2) Should the dict give you some kind of notification when it
overwrites a key/value pair during construction/display? Maybe. It
does seem a little illogical to write something in that will get
ignored, so I could imagine this giving a warning or error. The
question is probably: How much do you gain by having the dict check
for this, and how much effort is it therefore worth? Considering how
pervasive the dict is in Python's own internals, not to mention how
many times it's used in user-level code, any performance hit would
multiply out, so it would need to be extremely beneficial.

But it's a possibility for a linter, maybe.

ChrisA

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


Thread

Normal dict behavior? antoine <boolegue@gmail.com> - 2014-05-07 08:06 -0700
  Re: Normal dict behavior? Ned Batchelder <ned@nedbatchelder.com> - 2014-05-07 11:20 -0400
  Re: Normal dict behavior? Chris Angelico <rosuav@gmail.com> - 2014-05-08 01:22 +1000
  Re: Normal dict behavior? Christian Heimes <christian@python.org> - 2014-05-07 17:34 +0200
  Re: Normal dict behavior? Chris Angelico <rosuav@gmail.com> - 2014-05-08 01:38 +1000

csiph-web