On 3/8/06, w chun <[EMAIL PROTECTED]> wrote:
I prefer dicts for this sort of thing. That way "choice" could be something meaningful. For example, I recently wrote a python script showing how different sorting algorithms work (for my discrete math class). I have a dict of algorithms, something like (I'm typing from memory before my first cup of coffee - go gentle on me):
algs = {'quick':quicksort, 'merge':mergesort, "insert": insertionsort}
choice = "quick" # however we get the choice..eg from the command line.
if choice in algs:
choice(datatosort)
...
I really like not having to remember order. This way, I get the right function easily. And I can print out the list of keys to remind me which functions are available.
Anna
> def switch(choice, functions = [cf0, cf1, cf2, cf3])
should we really be showing folks using a mutable object as a default
argument? ;-)
i'd suggest: (1) changing it to a tuple, or (2) None and figure it out
after that.
I prefer dicts for this sort of thing. That way "choice" could be something meaningful. For example, I recently wrote a python script showing how different sorting algorithms work (for my discrete math class). I have a dict of algorithms, something like (I'm typing from memory before my first cup of coffee - go gentle on me):
algs = {'quick':quicksort, 'merge':mergesort, "insert": insertionsort}
choice = "quick" # however we get the choice..eg from the command line.
if choice in algs:
choice(datatosort)
...
I really like not having to remember order. This way, I get the right function easily. And I can print out the list of keys to remind me which functions are available.
Anna
_______________________________________________ Edu-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/edu-sig
