continuation lines

Guido.van.Rossum@cwi.nl
Wed, 12 May 1993 12:48:23 +0200

Someone gave me a tiny mod (about 15 lines) to the tokenizer that
makes breaking long lines easier. The mod makes it possible to omit
the backslash when breaking a line *inside parentheses*. The mod
keeps track of the accumulated nesting level of all sorts of
parentheses: () [] {}, and suppresses the NEWLINE token (and the check
for indentation level)when the parentheses nesting level is greater
than zero. It relies on the parser to make sure that parentheses
occur in properly matched pairs and at syntactically legal places.

The net effect is that you can write things like

function_with_many_arguments(the_first_argument,
the_second_argument,
the_third_argument,
the_fourth_argument,
the_last_argument)

or initialize lists as follows:

months = ['Jan', 'Feb', 'Mar', # first Q
'Apr', 'May', 'Jun', # second Q
'Jul', 'Aug', 'Sep', # third Q
'Oct', 'Nov', 'Dec'] # fourth Q

(Code using backslashes still works, but note that a line can't have a
backslash *and* a comment, so there's a definite improvement here.)

A drawback is that if you accidentally forget a closing parenthesis,
you might get a syntax error on the next line or even further down.
Usually this will be on the first token of the next line though (since
most tokens that can start a statement can't continue an expression --
the exception being open parentheses and brackets). A way to
alleviate this problem would be to print the entire current *logical*
line when a syntax error has occurred.

Ideally, one would like a syntax where a line can be broken without a
backslash anywhere where the syntax doesn't allow a newline token.
This would allow things like

if very_long_condition and _another_long_condition and
yet_another_condition and another_one_again:
do_something_very_carefully()

and

a_variable_with_an_excruciatingly_long_identifier =
an_expression_that_is_almost_equally_horrible

(but still not this:

print 1, 2, 3,
4, 5, 6

nor

x = 1, 2, 3,
4, 5, 6

-- do you see why not?)

However, such a change would require a much bigger change to the
parser and tokenizer (apart from the difficulties in spelling out
*exactly* what the rules would be, for future generations).

I feel that this mod would be a useful addition to Python (also it
fits nicely in the tradition of carefully trading certain principles
for implementability).

What do you think?

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>