あたりのよう
def f1():
return 1
def f2():
return 2
def f3():
return 3
def g(*ps):
functions = [f1, f2, f3]
for i, p in enumerate(ps):
if p == 1: # Could do just `if p:` (0 evaluates to False, anything else to True)
print(functions[i])() # Notice the() to actually call the function
if __name__ == "__main__":
print("Run 1 0 0")
g(1, 0, 0)
print("Run 1 1 0")
g(1, 1, 0)
print("Run 0 1 0")
g(0, 1, 0)
print("Run 1 1 1")
g(1, 1, 1)
:*args
を使用して(this、thisとthisを参照)、リストにあなたの機能を入れて(うん、Pythonはあなたがそれを行うことができます)など何かを得るリストのcommentをこの回答に追加すると、コードをもう少し短くすることもできます。例えば、zip
を使用して:
def g(*ps):
functions = [f1, f2, f3]
for function, p in zip(functions, ps):
if p:
print(function())
またはitertools.compress
を使用して(あなたは、ファイルの先頭にimport itertools
する必要があります):
def g(*ps):
functions = [f1, f2, f3]
for function in itertools.compress(functions, ps):
print(function())
'P1 == 1&P2 == 1&p3の場合== 1 '' f3() 'を実行したくないのですか? – BorrajaX
*など*?あなたがここに示していることから、望ましい行動が明らかであるようには思われません。 – Psidom
はい、あります。申し訳ありません、それはタイプミスでした。 –