在Python中解压dict时的“SyntaxError”

2024-06-13 13:21:04 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图运行pychromecast库的一个示例时,出现以下错误:

~/pychromecast $ python example.py 
Traceback (most recent call last):
File "example.py", line 11, in <module>
import pychromecast.controllers.youtube as youtube
File "/home/pi/pychromecast/pychromecast/controllers/youtube.py", line 27
REQUEST_PARAMS_SET_PLAYLIST = {**BASE_REQUEST_PARAMS, **SET_PLAYLIST_METHOD}
                                ^
SyntaxError: invalid syntax

我试过在Python2和Python3中运行这个。这里怎么了?在


Tags: py示例mostyoutubeexamplerequest错误line
1条回答
网友
1楼 · 发布于 2024-06-13 13:21:04

这个语法是在python3.5作为PEP 448 - Additional Unpacking Generalizations的一部分发布的。在

根据document:由于python3.5,tuple、list、set和dictionary显示允许多个解包,比如:

>>> *range(4), 4
(0, 1, 2, 3, 4)

>>> [*range(4), 4]
[0, 1, 2, 3, 4]

>>> {*range(4), 4, *(5, 6, 7)}
{0, 1, 2, 3, 4, 5, 6, 7}

>>> {'x': 1, **{'y': 2}}
{'x': 1, 'y': 2}

相关问题 更多 >