Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding long lines of code in Python

I try and keep my code within 80 characters wide so it is easy to see side by side in a standard window I set up. In doing this, I have a Python v2.7 construct like this:

subseq_id_to_intervals_dict, subseq_id_to_ccid_formats_dict, subseq_id_to_min_max_count_dict = map_cases(opts,
                                            format_to_ccid_funcs,
                                            sys.stdin)

So I broke it up like this:

subseq_id_to_intervals_dict,
subseq_id_to_ccid_formats_dict,
subseq_id_to_min_max_count_dict = map_cases(opts,
                                            format_to_ccid_funcs,
                                            sys.stdin)

But that caused errors

NameError: name 'subseq_id_to_intervals_dict' is not defined

Until I added backslashes:

subseq_id_to_intervals_dict,        \
subseq_id_to_ccid_formats_dict,     \
subseq_id_to_min_max_count_dict = map_cases(opts,
                                            format_to_ccid_funcs,
                                            sys.stdin)

Why is it that the comma at the end of the line does not inform Python sufficiently to understand the syntax and not get an error? Is there a cleaner way to do this without backslashes?

like image 294
WilliamKF Avatar asked Feb 28 '13 16:02

WilliamKF


People also ask

How do you avoid long lines in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.

Can you skip lines of code in Python?

There are many ways in which you can skip a line in python. Some methods are: if, continue, break, pass, readlines(), and slicing.

How do you break a large line in Python?

Use a backslash ( \ ) as a line continuation character In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, it is considered that the line is continued on the next line.

What happens when a line is too long in Python?

Breaking Long Lines of Code in Python Breaking lines increases the total number of lines of code. But at the same, it can drastically improve the readability of your code. It is recommended not to have lines of code longer than 79 characters.


1 Answers

You could put the left side of the assignment into parentheses:

(subseq_id_to_intervals_dict,
 subseq_id_to_ccid_formats_dict,
 subseq_id_to_min_max_count_dict) = map_cases(opts,
                                            format_to_ccid_funcs,
                                            sys.stdin)

The left side is already a tuple- the parentheses just imply the line continuation. The line

subseq_id_to_intervals_dict,

doesn't imply a line continuation because it is a complete statement- it's a tuple with a single element.

like image 162
David Robinson Avatar answered Oct 06 '22 00:10

David Robinson