Thoughts about Python
Mike C. Fletcher
mcfletch at rogers.com
Wed Feb 25 05:21:07 EST 2004
Marco Aschwanden wrote:
...
>'(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.
>
>
Actually, this is the classic problem of data-escaping. It's *possible*
to guarantee unique values when encoding as a string, but it's a *lot*
harder than just doing str( data ). You need to make sure that
different things are going to show up with different values:
d = {}
myWorkingList = (3,4,20.010100000000001)
somePieceOfDataFromSomewhere = '(3, 4, 20.010100000000001)'
d[ myWorkingList ] = 32
d[ somePieceOfDataFromSomewhere ] = 34
That is, you'd need to encode into your string information regarding the
*type* and internal structure of the object to avoid collisions where
something just happens to look the same as another object, so something
like:
'(I3\nI4\nF20.010100000000001\nt.' # for the tuple and
"S'(3,4,20.010100000000001)'\np1\n." # for the string
Thing is, creating those static string representations is a major PITA
(in case you're wondering, the above are pickles of the values), and
would waste a lot of memory and processing cycles (to build the safe
string representation, (a task which is somewhat involved)) compared to
just using a pointer to the object and the object's built-in __hash__
and comparison methods.
>It is also a bit complicated to retrieve the values from the string
>(it has to be "split"ted or "eval"ed back to a tuple or list).
>If this pattern would be seldom used... but it seems, that everybody
>except me uses this dicts as keys.
>
>
Round-trip-eval-capable objects are considered good style, but are not
by any means universal even among the built-in objects:
>>> x = object()
>>> x
<object object at 0x00C7D3B8>
pickle/cPickle is closer to the idea of a robust mechanism for coercion
to/from string values, but even it has its limits, particularly with
respect to older built-in types that assumed they'd never be pickled.
wrt sorting, it's not really a huge issue, dictionaries are not ordered,
so to produce a sorted set you'd need to produce the keys as a
list/sequence of reconstituted objects anyway.
Have fun, and enjoy yourself,
Mike
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list