I would like to ask a simple question about ternary operators. I came across this line of code
1)
variable_1 = variable_2 if condition else variable_1
where the else is the variable itself. If the condition is false, this causes assigning variable_1
value to itself, being somehow repetitive and depending on the interpreter optimizations maybe even slower.
In my particular example, this line was so long that for other style guides that had to be rewritten as:
2)
variable_1 = variable_2 if condition else \
variable_1
This could be rewritten as:
3)
if condition: variable_1 = variable_2
Now let's say you really don't like one-line-if, that could be rewritten as:
4)
if condition:
variable_1 = variable_2
(2) is always the best way to go for python despite the need to be split in two lines? Or would be better to use (3) or (4) in this case?
Which one would be the best style to use? (I don't really want to talk about your coding style preferences, is more about if there is any specific style-guide to reference, or performance problems)
Thank you!
Aucun commentaire:
Enregistrer un commentaire