On Jun 19, 12:40 pm, asincero [EMAIL PROTECTED] wrote:
Which is better: using an if/else construct to simulate a C switch or
use a dictionary? Example:
Whichever results in the clearest code that meets the performance
requirements.
FWIW, if you define the dictionary beforehand, the dict
asincero [EMAIL PROTECTED] wrote:
handle_case = {}
handle_case[1] = doCase1()
handle_case[2] = doCase2()
handle_case[3] = doCase3()
handle_case[4] = doCase4()
handle_case[5] = doCase5()
handle_case[c]()
If the switch values are simple integers then a list would be a
Ahh .. yes of course, you are right. I mis-typed. I like how you
defined the dictionary all in one statement, though. I didn't think
of doing it that way.
-- Arcadio
On Jun 19, 4:11 pm, heltena [EMAIL PROTECTED] wrote:
asincero ha escrit:
def foo():
def doCase1():
pass
Which is better: using an if/else construct to simulate a C switch or
use a dictionary? Example:
def foo():
if c == 1:
doCase1()
elif c == 2:
doCase2()
elif c == 3:
doCase3()
elif c == 4:
doCase4()
elif c == 5:
doCase5()
else:
raise shouldn't
asincero ha escrit:
def foo():
def doCase1():
pass
def doCase2():
pass
def doCase3():
pass
def doCase4():
pass
def doCase5():
pass
handle_case = {}
handle_case[1] = doCase1()
handle_case[2] = doCase2()
handle_case[3] =
asincero [EMAIL PROTECTED] wrote:
Which is better: using an if/else construct to simulate a C switch or
use a dictionary? Example:
Here is a technique I've used a lot. I think I learnt it from Dive
into Python
class foo(object):
def do_1(self):
print I'm 1
asincero [EMAIL PROTECTED] writes:
def foo():
def doCase1():
pass
def doCase2():
pass
def doCase3():
pass
def doCase4():
pass
def doCase5():
pass
handle_case = {}
handle_case[1] = doCase1()
handle_case[2] = doCase2()