I noticed that I wrote some python code, so make a note. It may be a rudimentary and common sense technique, but ...
Consider a case where you want to branch a variable called a depending on whether the result (some_condition + add_value) of a certain formula is larger than CONSTANT.
For the time being, I will write it appropriately with an if statement.
python
a = 0
if some_condition + add_value < CONSTANT:
a = some_condition + add_value
else:
a = CONSTANT
... I think it's too verbose, so I'll put the else operation in the initialization (variable declaration).
python
a = CONSTANT
if some_condition + add_value < CONSTANT:
a = some_condition + add_value
After doing this, I realized that all I had to do was compare some_condition + add_value with CONSTANT, so I used min to do just one line.
python
a = min(some_condition + add_value, CONSTANT)
There is no ternary operator in python, and the code tends to be long at the time of conditional branching, but if you use min / max, the code may be a little cleaner.
Recommended Posts