0
Python根本沒有執行switch/case
還是python開發人員假設使用一系列if, elif, else
語句呢?Python使用開關盒?
Python根本沒有執行switch/case
還是python開發人員假設使用一系列if, elif, else
語句呢?Python使用開關盒?
Python不執行switch
。一種替代方案是使用像這樣的字典:
def func1():
pass
def func2():
pass
switch = {
"do1": func1,
"do2": func2,
}
do_str = "do1"
switch[do_str]()
使用字典是一種優雅的方式,特別是如果你有「很多」的情況下(無論多麼可能對你來說)。對於少數選擇,使用更簡單的if .. elif .. else構造。這是Python文檔推薦的內容:https://docs.python.org/3/tutorial/controlflow.html – Cyb3rFly3r