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


Groups > comp.lang.python > #71032

Re: Normal dict behavior?

References <18f61e5d-f47b-4373-a835-67c6f20ef3d1@googlegroups.com>
Date 2014-05-08 01:22 +1000
Subject Re: Normal dict behavior?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.9739.1399476181.18130.python-list@python.org> (permalink)

Show all headers | 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