is int(round(val)) safe?

Tim Peters tim.peters at gmail.com
Tue Nov 23 15:13:00 EST 2004


[Steve Holden]
,,,
> >>> d = {1.0: "Something",
> ...        1: "Something else"}
> >>> d
> {1.0: 'Something else'}
> >>>
>
> Python has known for a long time that 1.0 and 1 are the same thing.
> Note, however, that I don't believe it's guaranteed that the contents of
> d will turn out the same in different Python versions. I suppose Tim
> would be able to quote chapter and verse, given his familiarity with
> every little implementation detail of the dict.

Alas, this chapter hasn't been written yet, let alone the verse.  If you do

d[k] = v

when d already has a key equal to k, its associated value is replaced,
but it's really not defined whether the old key is replaced or
retained.  All known implementations of Python retain the old key in
this case.

The other *seeming* ambiguity here isn't one:  whether, in {a: b, c:
d}, a or c is added first.  Python requires "left to right"
evaluation, so that's actually defined -- although this one may be
more clearly defined in Guido's head than by the docs.



More information about the Python-list mailing list