Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #71028 > unrolled thread
| Started by | antoine <boolegue@gmail.com> |
|---|---|
| First post | 2014-05-07 08:06 -0700 |
| Last post | 2014-05-08 01:38 +1000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | antoine <boolegue@gmail.com> |
|---|---|
| Date | 2014-05-07 08:06 -0700 |
| Subject | Normal dict behavior? |
| Message-ID | <18f61e5d-f47b-4373-a835-67c6f20ef3d1@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2014-05-07 11:20 -0400 |
| Message-ID | <mailman.9738.1399476055.18130.python-list@python.org> |
| In reply to | #71028 |
On 5/7/14 11:06 AM, antoine 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?
>
Because 0 == 0.0
--
Ned Batchelder, http://nedbatchelder.com
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-05-08 01:22 +1000 |
| Message-ID | <mailman.9739.1399476181.18130.python-list@python.org> |
| In reply to | #71028 |
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
[toc] | [prev] | [next] | [standalone]
| From | Christian Heimes <christian@python.org> |
|---|---|
| Date | 2014-05-07 17:34 +0200 |
| Message-ID | <mailman.9740.1399476883.18130.python-list@python.org> |
| In reply to | #71028 |
On 07.05.2014 17:20, Ned Batchelder wrote: > Because 0 == 0.0 > hash(0) == hash(0.0) and 0 == 0.0 Christian
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-05-08 01:38 +1000 |
| Message-ID | <mailman.9741.1399477117.18130.python-list@python.org> |
| In reply to | #71028 |
On Thu, May 8, 2014 at 1:34 AM, Christian Heimes <christian@python.org> wrote: > hash(0) == hash(0.0) and 0 == 0.0 In theory, the former should be implied by the latter. Any deviation from that is a bug in __hash__ for the two objects. ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web