Dict when defining not returning multi value key error

Ben Finney ben+python at benfinney.id.au
Fri Aug 1 00:17:41 EDT 2014


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:

> On Thu, 31 Jul 2014 20:12:12 -0700, Dan Stromberg wrote:
>
> > I removed some quotes, and noticed that 1 and 1.0 hash the same.
> > That's a bit unexpected, but I suppose it's not completely
> > unreasonable.

I would expect it, for the reasons Steven explains. Why is it
unexpected?

> You should expect that two equal objects should hash to the same value
> in different versions of Python, or even from one run of Python to the
> next.

I suspect there's a “not” missing there.

To be explicit: You should not expect any particular comparisons to hold
true for hash values between different Python versions, or even from one
run to the next.

Only those promises made or logically implied in the API should be
expected. Implementation details should be expected to break assumptions
— in other words, don't depend on any particular behaviour of an
implementation detail.

If you expect the hash valuesto be the same, you're making a mistake; if
you expect the hash values to be different, you're making a mistake.

The correct attitude, with regard to implementation details, is to
expect uncertainty and not assume anything :-)

-- 
 \      “Natural catastrophes are rare, but they come often enough. We |
  `\   need not force the hand of nature.” —Carl Sagan, _Cosmos_, 1980 |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list