[Python-ideas] Implicit String Concatenation
Adam Atlas
adam at atlas.st
Wed Apr 11 23:09:22 CEST 2007
On 11 Apr 2007, at 16.15, Jim Jewett wrote:
> I would support a proposal to remove the implicit concatenation
> entirely.
I'd agree with that. The parser can probably just do the same
optimization automatically if it gets [string literal] "+" [string
literal]. (Or does it already?)
Meanwhile, on a similar subject, I have a... strange idea. I'm not
sure how easy/hard it would be to parse or how necessary it is, but
it's just a thought.
Currently, you can do multiline strings a couple of ways:
x = '''foo
bar
baz'''
x = 'foo' \
'bar' \
'baz'
Neither of these seem ideal. Triple-quoting is decent, but it can get
ugly if you're using it in an indented block (as you most often will
be), since the following lines are considered to start right after
the newline, not after the containing block's indentation level. But
changing it to the latter behaviour has been discussed before, if I
remember correctly, and that didn't seem popular. That's
understandable; the current triple-quote multiline behaviour makes
sense from a logical point of view, it just doesn't look as nice to
have text suddenly fall down to 0 indentation and then jump back to
the original indentation level when the quote is over. So anyway,
what I'm proposing is the following:
x = 'foo
'bar
'baz'
In other words, if you start a ' or "-quoted string, and don't close
it at the end of the line, you can continue it on the next line. It
would be generally equivalent to appending \n, closing the quote, and
preceding the physical newline with a backslash. (And inserting a
plus sign, if we take Jim's proposal into account.) Not closing a
quote and doing something else on the next line (i.e. not starting it
with a quote character after any whitespace) would still be a syntax
error.
This style takes precedent from multi-paragraph quoting style in
English: if you end a paragraph without closing a quote, then you
continue it by starting the next one with a quote, and you can
continue like that until you do have an end-quote.
I think it would improve readability/writability for when you need to
include multiline text blocks or code blocks. Having to have that \n"+
\ at the end of each line really breaks up the flow, whether of a
block of human or computer language text. And having subsequent lines
fall to 0 indentation (if you choose to use triple-quotes) breaks up
the flow of the surrounding Python code. This seems like a good
solution, especially since it has precedent in written English. Any
thoughts?
More information about the Python-ideas
mailing list