色粉盒撒娇 发表于 2017-4-26 07:58:57

python中的switch

1.
class MyCommandDispatcher(object):
def do_a(self):
# do stuff
def do_b(self):
# do stuff
def do_5(self):
# do stuff
def default(self):
# do stuff
def switch(self, option):
getattr(self, 'do_' + str(option), self.default)()
d = MyCommandDispatcher()
d.switch('a')
d.switch(5)

2.
result = {
'a': lambda x: x * 5,
'b': lambda x: x + 7,
'c': lambda x: x - 2
}(x)

3.if
elif
else
页: [1]
查看完整版本: python中的switch