Yes, getQ
would be called twice if the result is nonnegative, so the second would definitely be quicker in that case.
In this particular case, you could write x = max(getQ(x), 0)
. The "general" solution you're looking for is something like
x = (lambda x: x if x >= 0 else 0)(getQ(x))
I wouldn't recommend using this in real code, but it lets you bind a name while still keeping it one expression.