A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2004-July/046040.html below:

[Python-Dev] Equal but different keys in dicts

[Python-Dev] Equal but different keys in dictsMichael Chermside mcherm at mcherm.com
Sun Jul 11 05:06:12 CEST 2004
Suppose you create two objects which are, in fact, different, but which are ==
to each other.

>>> class AllEqual:
...     def __init__(self, name):
...         self.name = name
...     def __repr__(self):
...         return self.name
...     def __eq__(self, other):
...         return 1
...     def __hash__(self)
...         return 47
...
>>> a = AllEqual('a')
>>> b = AllEqual('b')
>>> a == b
1

Now use one as a key in a dict, then try re-setting that value:

>>> map = {a:'one'}
>>> map
{a: 'one'}
>>> map[b] = 'two'
>>> map
{a: 'two'}

That last line could just as well have read "{b: 'two'}", but the implementation
of dict keeps the ORIGINAL key rather than using the NEW key. This is the
behavior of both CPython and Jython.

My question is this: is this behavior intentional, or is it an "implementation
detail"? If intentional, is there a reason for the choice, or is it just that
one or the other behavior needed to be chosen?

[PS: I'm hoping that a good answer to this question will allow me to close bug
748126]

-- Michael Chermside

More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4