Suppose that I have a numeric variable which will have another value added to it, but I want to make sure that this variable never exceeds a maximum value, and upon being exceeded, will simply default to the maximum value.
For example, given a maximum value of 100, and a function which does this which is called maxadd:
input1 = 90
input2 = 8
maxadd(input1, input2, 100)
>>> 98
input1 = 95
input2 = 8
maxadd(input1, input2, 100)
>>> 100
I could just define it as a normal function like this:
def maxadd(a, b, _max):
res = a + b
if res > _max:
return _max
return res
But I feel like it could be done in a single line, maybe with a lambda. I can't seem to figure anything out though. Performance is also a concern so I would like the fastest solution possible, and I feel like this function may be taking unnecessary steps