Python 3.0, rich comparisons and sorting order

Alex Martelli aleaxit at yahoo.com
Wed Sep 22 09:20:55 CEST 2004


Steven Bethard <steven.bethard at gmail.com> wrote:
   ...
> (2) "is there a good use case for wanting to make a mapping with keys that
> have incompatible types?" (my question to you)
> 
> To some degree, (1) has already been answered to my satisfaction by Carlos
> Ribeiro's spreadsheet example.  If you could give me a real world example of
> when you'd want to do (2), I might be more convinced...

Does my example of 'tuples as concrete representations of expressions'
which I posted to another subthread of this thread help?  Say several
such expressions are coming in to be evaluated over the same context
(assignment of values to free variables), I want to memoize the
expressions that I have already computed this time (to avoid recomputing
in a situation where the same expression or subexpression is likely to
recur ofter) and the natural way to do it is:
    if expr in known_expr: return known_expr[expr]
    value = known_expr[expr] = do_compute(expr)
    return value

In this case a dict would do just as well as a BTree, but it's easy to
think of a slight variant where expressions would not easily be made
hashable, but might still easily use some arbitrary comparison.


Alex
 



More information about the Python-list mailing list