Thoughts about Python

Terry Reedy tjreedy at udel.edu
Wed Feb 25 11:20:42 EST 2004


"Marco Aschwanden" <PPNTWIMBXFFC at spammotel.com> wrote in message
news:15c2d03b.0402250123.151f06db at posting.google.com...
> cookedm+news at physics.mcmaster.ca (David M. Cooke) wrote in message
news:<qnk4qtgt7j9.fsf at arbutus.physics.mcmaster.ca>...
> >>> tup = (3, 4, 20.0101)
> >>> stringified = str(tup)
> >>> stringified
> '(3, 4, 20.010100000000001)'  # 8o)
> What is not unique about this string?
> Okay, I can think of cases where this approach is problematic -
> foremost when sorting is involved.

Mike pointed out the problem of making sure that different things get
different strings.  For dict keys, there is also the problem of making sure
that 'different' things that hash equal and which have the same value as a
key get the same string, and you cannot do that without inventing a new
*non-invertible* stringify function.  In particular, numbers are hashed by
value, with 0 == 0L == 0.0 => hash(0) == hash(0L) == hash(0.0), whereas
these would give three different strings with either str() or repr().
Example:

>>> d={1:'one'}
>>> d[1.0]
'one'

So, you make newstr((1,2)) == newstr((1.0,2L)).  Which means you must
eliminate the number type information and make newstr *not* invertible!  Or
you could change the dict key rules;=).

Terry J. Reedy








More information about the Python-list mailing list