Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a line break on the Python ternary operator?

Sometimes a line containing a ternary operator in Python gets too long:

answer = 'Ten for that? You must be mad!' if does_not_haggle(brian) else "It's worth ten if it's worth a shekel." 

Is there a recommended way to make a line break at 79 characters with a ternary operator? I did not find it in PEP 8.

like image 814
nedim Avatar asked Mar 06 '15 10:03

nedim


People also ask

How do you break a ternary operator in Python?

break is a statement and not an expression, so it can't be used inside a ternary conditional expression. -from Google. for (let i = 0; i < 10; i++) { document. write(i); } Same thing you wanted to do,here's no reason in ternary operator.

How do you do a line break in Python?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.

Can ternary operator return string?

Ternary operator values The values part of the ternary operator in the above example is this: “This is an even number!” : “This is an odd number!”; In the example above, if the condition evaluates to true then the ternary operator will return the string value “This is an even number!”.

Is ternary operator Pythonic?

Ternary operators are also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.


2 Answers

You can always extend a logical line across multiple physical lines with parentheses:

answer = (     'Ten for that? You must be mad!' if does_not_haggle(brian)     else "It's worth ten if it's worth a shekel.") 

This is called implicit line joining.

The above uses the PEP8 everything-indented-one-step-more style (called a hanging indent). You can also indent extra lines to match the opening parenthesis:

answer = ('Ten for that? You must be mad!' if does_not_haggle(brian)           else "It's worth ten if it's worth a shekel.") 

but this leaves you hitting the 80-column maximum all the faster.

Where precisely you put the if and else portions is up to you; I used my personal preference above, but there is no specific style for the operator that anyone agrees on, yet.

like image 156
Martijn Pieters Avatar answered Sep 20 '22 04:09

Martijn Pieters


PEP8 says the preferred way of breaking long lines is using parentheses:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

answer = ('Ten for that? You must be mad!'           if does_not_haggle(brian)           else "It's worth ten if it's worth a shekel.") 
like image 34
Peter Wood Avatar answered Sep 19 '22 04:09

Peter Wood