I do not think this is possible in Python, because what you are actually trying to do is probably expanding to the following:
num1 = 20 if someBoolValue else num1
If you exclude else num1 , you will get a syntax error, as I am pretty sure that the assignment really should return something.
As mentioned above, you can do this, but itβs bad, because you will probably just stop confusing yourself when you read this piece of code the next time:
if someBoolValue: num1=20
I'm not a big fan of num1 = someBoolValue and 20 or num1 for the same reason. I have to think twice about what this line does.
The best way to achieve what you want to do is with the original version:
if someBoolValue: num1 = 20
The reason the best check is because it is very obvious what you want to do, and you wonβt confuse yourself, or anyone else will come into contact with this code later.
Also, as a side note, num1 = 20 if someBoolValue is valid Ruby code, because Ruby works a little differently.
Frost Oct 24 '11 at 8:27 2011-10-24 08:27
source share