Python 使用 'with' 的语法错误

Python syntax error using 'with'

我正在尝试编译一个项目,但在测试期间出现无效语法错误,箭头指向 'h' 中的 with。我还没有写过代码,它已经有好几年了。

d = Gnuplot.Data(pnts,title=im_title,with='candlesticks')

我尝试将 with 更改为其他内容,但随后出现了不同的错误。我该怎么做才能解决这个问题?

显然这段代码是在 with 成为 reserved keywords 之一之前编写的。

(可能的)解决方法:

d = Gnuplot.Data(pnts, **{'title': im_title, 'with': 'candlesticks'})

刚刚检查过,它不会工作 - they were using with as a variable name extensively until Gnuplot.py 1.8

Gnuplot.py 1.8+ 的解决方案是使用 with_ 参数:

d = Gnuplot.Data(pnts, title=im_title, with_='candlesticks')

在 PEP 8 - Python 代码风格指南中,以下指南出现在 Descriptive: Naming Styles 部分:

single_trailing_underscore_ : used by convention to avoid conflicts with Python keyword

即:

d = Gnuplot.Data(pnts,title=im_title,with_='candlesticks')