tags:

views:

187

answers:

5

Why on Earth doesn't the interpreter raise SyntaxError everytime I do this:

my_abc = ['a',
          'b',
          'c'
          'd',]

I just wanted to add 'c' to the list of strings, and forgot to append the comma. I would expect this to cause some kind of error, as it's cleary incorrect.

Instead, what I got:

>>> my_abc
 ['a', 'b', 'cd']

And this is never what I want.

Why is it automatically concatenated? I can hardly count how many times I got bitten by this behavior. Is there anything I can do with it?

Just to clarify*: I don't actually mind auto-concatenation, my problem has to do ONLY with lists of strings, because they often do much more than just carry text, they're used to control flow, to pass field names and many other things.

+6  A: 

It's by design. It allows, for example, writing long string literals in several lines without using +.

wRAR
I'd rather use """ for multilne strings or break them explicitly with a `\`, than than this kind of *magic* concatenation.
kurczak
""" puts newlines between lines.
recursive
It's not "magic". It's well-documented: http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation
S.Lott
@recursive you re right, sorry
kurczak
+2  A: 

Because two string literals side-by-side, delimited by whitespace, are concatenated. Since the strings are within a list, they are 'side-by-side'.

See: http://docs.python.org/reference/lexical%5Fanalysis.html#string-literal-concatenation

Nick Presta
Not sure why this was downvoted. Am I incorrect in some way?
Nick Presta
The question is *why*. You've simply restated what is occuring, which the OP already has in his post. If a question asks "Why does 10*2 = 20", the answer "because 10*2 = 20" is not helpful.
John Millikin
The _why_ is explained in the docs - it is by design and has use cases.
Nick Presta
+2  A: 

Because often people want to do something like this:

line = ("Here's a very long line, with no line breaks,"
        " which should be displayed to the user (perhaps"
        " as an error message or question box).")

It's easier to write this without having to manually concatenate strings. C, C++, and (I believe) Java and C# also have this behavior.

John Millikin
""" does not do this. It inserts newlines.
recursive
@kurczak: 40 years of developers disagree with you.
John Millikin
@John Millikin please elaborate
kurczak
Automatic string concatenation has been present in most languages since at least C, which was released roughly 40 years ago. While age of a feature doesn't necessarily mean it's a good idea, the fact that it's 1) survived unchanged in dozens of descendant languages 2) been adopted by unrelated languages indicates that it's generally regarded as a good idea.
John Millikin
+11  A: 

Is called "Implicit String Concatenation" and a PEP that proposed its removal was rejected: http://www.python.org/dev/peps/pep-3126/

mandel
+1 Not mush to say after reading that link.
P-A
+2  A: 

As others said, it's by design.

Why is it so ? Mostly for historical reasons : C also does it.

In some cases it's handy because it reduce syntaxic noise and avoid adding unwanted spaces (inline SQL queries, complexes regexpes, etc).

What you can do about it ? Not much, but if it really happens often for you, try one of the following tricks.

  • indent your list with coma at the beginning of the line. It's weird, but if you do so the missing comas become obvious.
  • assign strings to variables and use variables list whenever you can (and it's often a good idea for other reasons, like avoiding duplicate strings).
  • split your list: for list of words you can put the whole list inside only one string and split it like below. For more than 5 elements it's also shorter.

    'a b c d e'.split(' ').

kriss
Or put a comma AFTER each item (including the last one); the interpreter allows the extra comma. That way if you format your list as one value per line and move items up or down you won't run into this issue.
Dawie Strauss
That seems to be what did the original question poster (and that's also what I usually do for the reason you just said), but a missing coma at end of line can easily go unseen. The opposite problem also occured to me : a trailing coma at end of line changing a constant into tuple. I believe this one is even harder to detect than the other :-(
kriss
accepted for best effort ;]
kurczak