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?
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.
There are many ways in which you can skip a line in python. Some methods are: if, continue, break, pass, readlines(), and slicing.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With