0

Is there a PEP suggestion for the use of the ternary operator?

12th Sep 2020, 2:26 PM
Tim
Tim - avatar
2 Respuestas
+ 3
PEP8 does not specifically mention the ternary operator, but it contradicts with the principle that each statement should be written in a separate line. https://www.python.org/dev/peps/pep-0008/#other-recommendations - find "compound statements" See also: https://stackoverflow.com/questions/35070436/python-ternary-style-is-this-good-or-bad-style
12th Sep 2020, 3:35 PM
Tibor Santa
Tibor Santa - avatar
+ 2
yeah as long as it only contains one if statement: x = "Enough" if len(lst) > 5 else "Need more" However, if it contains multiple if statements then that makes it hard to read and therefore should be avoided.
12th Sep 2020, 2:32 PM
Bagon
Bagon - avatar