Надеюсь, это поможет:
Вариант 1:
class MyTestClass:
def loopTest(self, type, value, value2):
# necessary to iterate over the values later on
possibleArgs = [value, value2]
conditions = [type == 'YES', type == 'MAYBE', type=='TRY AGAIN']
# here you made the mistake that the first lambda expression didnt call the function "_f" it just "looked" at the reference
sentences = [lambda: self._f(), lambda x: self._f2(x), lambda x,y: self._f3(x, y)]
for condition, sentence in zip(conditions, sentences):
if condition:
# necessary to pass the arguments to the function
# "sentence.__code__.co_argcount" gives us the number of arguments of the function
args = (possibleArgs[i] for i in range(sentence.__code__.co_argcount))
sentence(*args)
break
def _f(self):
print('executed')
def _f2(self, x):
print(f'executed with value {x}')
def _f3(self, x, y):
print(f'executed with values {x} and {y}')
MyTestClass().loopTest("TRY AGAIN", 5, 2)
# output: executed with values 5 and 2
Вариант 2:
class MyTestClass:
def loopTest(self, type, value, value2):
a = value
b = value2
conditions = [type == 'YES', type == 'MAYBE', type=='TRY AGAIN']
# here you made the mistake that the first lambda expression didnt call the function "_f" it just "looked" at the reference
sentences = [lambda: self._f(), lambda: self._f2(a), lambda: self._f3(a, b)]
for condition, sentence in zip(conditions, sentences):
if condition:
sentence()
break
def _f(self):
print('executed')
def _f2(self, x):
print(f'executed with value {x}')
def _f3(self, x, y):
print(f'executed with values {x} and {y}')
MyTestClass().loopTest("TRY AGAIN", 5, 2)
# output: executed with values 5 and 2