Expect like syntax in Python

Jeff Shannon jeff at ccvcorp.com
Mon Feb 4 19:01:04 EST 2002


Guy Gascoigne - Piggford wrote:

> Neat.  The dictionary idea might be tidier - I'll try it out.  The only
> thing that I'm unsure about is that with a list there is a predictable order
> in which the list is iterated and as such there is a clear order in which
> the patterns are tested.  With a dictionary I assume that whilst the order
> is constant, it's not at all obvious from the declaration what it'll be.

This is true.  Dictionaries are, I believe, internally "ordered" by the hash
order of their keys--constant, but essentially random.  Depending on your usage,
though, you may be able to extract the list of keys, sort that, then iterate
over the sorted list:

keys = MyDict.keys()
keys.sort()
for key in keys:
    if received == key:
        MyDict[key]()

or something of that sort.  (Spice with an appropriate cmp() function to taste
;) )


> As for the clarity of anonymous functions, I've seen it go both ways.  Where
> they are short I think that they can be very useful, but you're right, as
> they get longer they can become very distracting.

Yes, it is a bit of a tradeoff, and the exact point at which anonymous functions
cease to be beneficial is a matter of taste.  My own taste puts that point
pretty low, but judging by the number of lambda-proponents in this group, my
opinions don't necessarily represent any sort of concensus.  ;)

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list