[Python-3000] Line continuation using ellipsis
Ron Adam
rrr at ronadam.com
Sat Apr 14 03:17:03 CEST 2007
Andrew Koenig wrote:
>> I'm not proposing to remove the feature, however I'd like to see an
>> alternative method for declaring statements that cross a line boundary.
>> I seem to recall at one point someone suggesting the use of ellipsis,
>> which makes a lot of sense to me:
>>
>> sorted_result = partition_lower( input_list, pivot ) ...
>> + pivot ...
>> + partition_upper( input_list, pivot )
>
> I once used a programming language named EFL that had what I think is a nice
> convention: If the last token on a line is an operator (i.e. something that
> cannot syntactically be the last token of a statement), the next line is
> deemed a continuation. So your example would be able to be written this
> way:
>
> sorted_result = partition_lower( input_list, pivot) +
> pivot +
> partition_upper( input_list, pivot )
>
> Interestingly, if I remember correctly, EFL did *not* assume a continuation
> for unmatched parentheses -- it looked only at the last token on the line.
>
> For Python, I guess I'd like it if either unmatched parentheses or ending
> with an operator were to signal continuation.
I tend to do it the other way around because starting off each continued
line with an operator makes for more readable code. It's even nicer with
syntax highlighting.
Using either a backslash or parentheses is fine for me in those situations.
The attractiveness of the ellipsis is it is already used in an interactive
console to indicate continued lines, but you still need the slash or
parentheses to get that. ;-)
The only problem I have with the '\' is I don't like it's behavior in
strings. I would prefer that a single back slash in regular strings to
always be an escape introducer and cause a syntax error if it's an invalid
escape character or it is not at the end of a line, and for it to always be
a single back slash in raw strings, except at the end of a physical line,
where it can be a continue line indicator as usual.
>>> r"\"
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
>>> "\""
'"'
>>> r"\""
'\\"'
Shouldn't r"\" result in "\\" ?
>>> print '1 \ 2'
1 \ 2
>>> print '1 \\ 2'
1 \ 2
>>> print '1 \\\ 2'
1 \\ 2
>>> print '1 \\\\ 2'
1 \\ 2
>>> """ hello
... world"""
' hello\nworld'
>>> """ hello \
... world"""
' hello world'
>>> """hello\
... world"""
'hello\\ \nworld'
I think this does result in silent bugs at times, but more often result in
the output not being what was expected.
Cheers,
Ron
More information about the Python-3000
mailing list