Tenete a mente questi consigli da Lo Zen di Python: "conta leggibilità"
L'operatore ternario è più leggibile quando è tutto su una riga.
x = y if z else w
Quando le vostre condizioni o variabili spingere la linea oltre 79 caratteri (vedi PEP8), leggibilità comincia a soffrire. (La leggibilità è anche il motivo per cui le dict/list comprehensions sono meglio conservate.)
Quindi, piuttosto che tentare di interrompere la linea usando le parentesi, potresti trovare che sia più leggibile se lo converti in un normale blocco if
.
if does_not_haggle(brian):
answer = 'Ten for that? You must be mad!'
else:
answer = "It's worth ten if it's worth a shekel."
BONUS: I refactoring rivela un altro problema leggibilità: logica does_not_haggle
viene invertito. Ciò sarebbe ancora più leggibile, se è possibile riscrivere la funzione:
if haggles(brian):
answer = "It's worth ten if it's worth a shekel."
else:
answer = 'Ten for that? You must be mad!'
fonte
2017-10-06 13:26:01
Inserirlo tra parentesi. –