efficiency question
Scott David Daniels
scott.daniels at acm.org
Fri Jun 30 22:11:34 EDT 2006
David Harvey wrote:
> if x in ("abc", "def", "xyz"):
> doStuff()
> elif x in ("pqr", "tuv", "123"):
> doOtherStuff()
> elif ...
If the code really looks like this:
# one-time-only code
big_dispatch_table = {}
for function, keys in [
(doStuff, ["abc", "def", "xyz"]),
(doOtherStuff, ["pqr", "tuv", "123"]),
... ]:
for key in keys:
big_dispatch_table[key] = function
Then your actual code goes something like:
big_dispatch_table[x]()
--Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list