Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with the syntax of this simple Python list?

Maybe I've gotten rusty with Python. Why is this not acceptable when pasted into a Python shell?

hdr_filenames = [
    "20210311_105300_HDR.jpg",
    "20210311_105306_HDR.jpg",
    "20210311_105310_HDR.jpg",
    "20210311_105314_HDR.jpg",
    "20210311_105341_HDR.jpg",    # order of last two have reversed exposures   
    "20210311_105323_HDR.jpg"
    ]

When this is copied to the prompt in an xterm running python3, I get (never mind the quaint retro term):

enter image description here

EDIT: silly of me to forget to report some basic obvious info: This is Python version 3.9.1, on Arch Linux updated about a month or so ago.

like image 299
DarenW Avatar asked Mar 12 '21 07:03

DarenW


People also ask

Why is there SyntaxError in Python?

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.

What is SyntaxError in Python with example?

Syntax errors are mistakes in the use of the Python language, and are analogous to spelling or grammar mistakes in a language like English: for example, the sentence Would you some tea? does not make sense – it is missing a verb. Common Python syntax errors include: leaving out a keyword.


1 Answers

So, I don't have a complete answer here, but this has something to do with the new PEG parser that debuted in Python 3.9, because when I use it (it is the default parser on Python 3.9), I get the same error:

Python 3.9.1 (default, Dec 11 2020, 06:28:49)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hdr_filenames = [
    "20210311_105300_HDR.jpg",
    "20210311_105306_HDR.jpg",
    "20210311_105310_HDR.jpg",
    "20210311_105314_HDR.jpg",
    "20210311_105341_HDR.jpg",    # order of last two have reversed exposures
    "20210311_105323_HDR.jpg"
    ]
  File "<stdin>", line 1
        ]
         ^
SyntaxError: multiple statements found while compiling a single statement

BUT You can revert to the old, LL(1) parser by passing a command line option, an voila! No error:

(py39) juanarrivillaga@50-254-139-253-static % python -X oldparser
Python 3.9.1 (default, Dec 11 2020, 06:28:49)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hdr_filenames = [
    "20210311_105300_HDR.jpg",
    "20210311_105306_HDR.jpg",
    "20210311_105310_HDR.jpg",
    "20210311_105314_HDR.jpg",
    "20210311_105341_HDR.jpg",    # order of last two have reversed exposures
    "20210311_105323_HDR.jpg"
    ]
>>> exit()

For those interested, the relevant PEP 617 about the new parser.

EDIT

So, this no-longer seems to be a problem on Python 3.9.2 (the latest version currently, I believe). So perhaps upgrade?

Python 3.9.2 (default, Mar  3 2021, 11:58:52)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hdr_filenames = [
...     "20210311_105300_HDR.jpg",
...     "20210311_105306_HDR.jpg",
...     "20210311_105310_HDR.jpg",
...     "20210311_105314_HDR.jpg",
...     "20210311_105341_HDR.jpg",    # order of last two have reversed exposures
...     "20210311_105323_HDR.jpg"
...     ]
>>>
like image 91
juanpa.arrivillaga Avatar answered Nov 15 '22 18:11

juanpa.arrivillaga