is int(round(val)) safe?

Peter Hansen peter at engcorp.com
Wed Nov 24 09:00:02 EST 2004


Tim Peters wrote:
> [Steve Holden]
>>Note, however, that I don't believe it's guaranteed that the contents of
>>d will turn out the same in different Python versions. 
 >
> 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.

Leading to very interesting results like this:

 >>> d = { 1: 'a', 1.0: 'b' }
 >>> d
{1: 'b'}

Perhaps unexpected, but clearly explained by the comments above...

-Peter



More information about the Python-list mailing list