This might be a basic question because I am still learning Python. But let's suppose I want to do something like this,
def add(a, b):
return a + b
a, b, c, d = 1, 2, 3, 4
sum = add(a, b if b == 2 else c, d)
Basically, I am trying to pass multiple functional args from inside a ternary operator(and I want a similar solution, if possible). This code currently gives the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: add() takes 2 positional arguments but 3 were given
What else have I tried:
add((a, b) if b == 2 else (c, d))
add(*(a, b) if b == 2 else *(c, d))
question from:
https://stackoverflow.com/questions/65852119/pass-functional-arguments-through-ternary-operator-in-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…