I wonder, is it possible to achieve similar using bit operations:
if a > maximum: a = maximum
Where ‘maximum’ can be a random number?
Have many similar lines in my current code. Of course could have used:
def foo(a, max=512): return a if a<max else max
Just curious if there’s a more elegant and efficient way.
There’s no need to define your own function for this,
minandmaxare already built-in:As per Raymond’s answer, it is also possible to use bit operations:
But in the vast majority of cases, the performance benefit isn’t really worth making the code very hard to understand. Also, this only works for integers, whereas the
minfunction can be used for all comparable types.