Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with list of strings in python

Tags:

python

list

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.

like image 479
kurczak Avatar asked Sep 09 '09 19:09

kurczak


2 Answers

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

like image 51
mandel Avatar answered Oct 01 '22 06:10

mandel


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

like image 37
wRAR Avatar answered Oct 01 '22 07:10

wRAR