我有一個依賴於argparse的腳本。腳本的主體有這樣的陳述:if語句中的幾個參數
if results.short == True and results.verbose == False and results.verbose2 == False and results.list == False and results.true == False:
是否有任何簡短的方法來做到這一點?假設我有超過5個參數,在每個陳述中輸入每個參數似乎都是重複的工作。
不能,如果這樣做:
if results.short == True and "results.%s"== False % (everyotherresults.something):
我寫的Python 2.7
只需使用'如果results.short而不是%(東西)' 「的結果%s的。」但是,如果它很麻煩,你可以把所有的情況放在一本字典中並遍歷它。 –