While working my way through Learning Python, this piece of code works fine in Spyder, but breaks in iPython:

# Create a sequence
sequence = ['A', 'B', 'C', 'D']

# Create two variables, the first with 'A', the second with the rest of the letters
Alpha, *allOtherLetters = sequence

When I try to run it in iPython, I get this error:

File "<ipython-input-3-b15e0241bc60>", line 5
    Alpha, *allOtherLetters = sequence
           ^
SyntaxError: invalid syntax

Clearly iPython doesn't like the *, but I don't know what to do about it.

有帮助吗?

解决方案

Your IPython probably uses Python 2. foo, *bar = ... only works in Python 3. You can have a look at sys.version_info.major to check which Python version you are using.

Tee solution is to install IPython for Python 3. Since I don't know which distribution you use the easiest way might be creating a Python 3 venv and installing ipython in there using pip install ipython.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top