Python Forum
string literals in a list. Not what I expected.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string literals in a list. Not what I expected.
#1
Hi

I saw unexpected Python behavior (to me) with string literals in a list. If I miss a comma in the list sequence, then the two string literals are merged into on string. What I had expected was a syntax error.

In the example below, if I remove the comma between the 'e' and 'i', the list merges the two into 'ei'. The missing comma acts as an implicit + operator.

If I miss a comma between a variable a string literal, or put two commas in a row, I get the Python syntax error I expect.

C:\Users\me>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> ['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o', 'u']

>>> ['a', 'e' 'i', 'o', 'u']
['a', 'ei', 'o', 'u']

>>> ['a', 'e' + 'i', 'o', 'u']
['a', 'ei', 'o', 'u']

>>> s = 'b'; ['a', 'e', s, 'i', 'o', 'u']
['a', 'e', 'b', 'i', 'o', 'u']

>>> s = 'b'; ['a', 'e', s 'i', 'o', 'u']
  File "<stdin>", line 1
    s = 'b'; ['a', 'e', s 'i', 'o', 'u']
                          ^
SyntaxError: invalid syntax

>>> s = 'b'; ['a', 'e', , 'i', 'o', 'u']
  File "<stdin>", line 1
    s = 'b'; ['a', 'e', , 'i', 'o', 'u']
                        ^
SyntaxError: invalid syntax

>>>
I want to confirm this merging of literals is correct Python behavior.

To me, Python interpreting the missing comma as an implicit append operator seems error prone. It would be hard to spot a missing comma would be hard to spot in a large list of string literals and a cause of programming bugs.
Reply
#2
Python documentation: String literal concatenation
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
This is more than I want to read right now, but perhaps you will find your answer:
https://docs.python.org/3/library/stdtyp...operations

I was unaware of this behaviour, thus don't know if legal or not. I would expect that after all this time, having not been changed, that there is a perfectly good reason for this behaviour.
If you don't find one, place a query on python.org to the authors of python.
Reply
#4
@perfringo
Thanks for the link. So yes it is legal.

Though in my opinion it is dangerous and easily introduces errors. A pity the '+' operator is only required for runtime but not for compile time. A gotcha for Python coding.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replace in code vs literals Skaperen 3 2,398 Mar-30-2021, 12:37 AM
Last Post: Skaperen
  What Are Python Literals? Julie 11 4,342 Sep-23-2020, 05:31 PM
Last Post: Gribouillis
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 18,523 May-08-2020, 04:26 AM
Last Post: bowlofred
  parenthesis around a tuple of literals in a for Skaperen 2 2,142 Aug-25-2019, 03:00 AM
Last Post: Skaperen
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,202 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  Create Alert if string from list appears on other list javalava 1 2,479 Sep-17-2018, 02:44 PM
Last Post: DeaD_EyE
  Python error: TypeError: expected string or buffer chris0147 1 6,617 Feb-23-2018, 02:42 AM
Last Post: Larz60+
  List of pathlib.Paths Not Ordered As Same List of Same String Filenames QbLearningPython 20 15,177 Nov-16-2017, 04:47 PM
Last Post: QbLearningPython
  Create a new list by comparing values in a list and string DBS 2 3,485 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020