恩…很遗憾, 貌似python没有switch
但是我们可以用字典来实现switch的功能!
举个简单的例子:
def fun1():
print("do one thing")
def fun2():
print("do another thing")
dict = {'1':fun1,'2':fun2}
dict.get('2')()
本文介绍了一种在Python中模拟switch语句的方法:利用字典映射不同选项到对应的函数。通过具体示例展示了如何定义多个函数并将其作为字典值,从而通过键来调用不同的函数。
恩…很遗憾, 貌似python没有switch
但是我们可以用字典来实现switch的功能!
举个简单的例子:
def fun1():
print("do one thing")
def fun2():
print("do another thing")
dict = {'1':fun1,'2':fun2}
dict.get('2')()

被折叠的 条评论
为什么被折叠?