转义保留关键字 Python

tka*_*ara 1 python netsuite python-3.x suitetalk zeep

我正在使用 ZEEP 连接到 NetSuite。创建发票时我需要传递给 NS 的参数之一是“类”。如果我理解正确,以下行无法编译的原因是因为 'class' 是保留关键字。

invoice = invoiceType(
    customFieldList = customFieldList,
    entity = entityRecord,
    subsidiary = subRecord,
    department = departmentRecord,
    location = locationRecord,
    class = classRecord
)
Run Code Online (Sandbox Code Playgroud)

我没有选择将最后一个参数从“类”更改为“类”或其他内容,因为这是 NetSuite 期望调用的参数。我可以在 python 中使用任何替代方法吗?有没有办法在将其作为参数传递时对其进行转义?

che*_*ner 5

您需要使用**{...}语法来传递名称为保留字的关键字参数。

invoice = invoiceType(
              customFieldList=customFieldList, 
              entity=entityRecord,
              subsidiary=subRecord,
              department=departmentRecord,
              location=locationRecord,
              **{'class': classRecord}
           )
Run Code Online (Sandbox Code Playgroud)

这样做是创建一个带有调用键'class'的字典,然后将字典展开为参数,这样您就不必指定文字class关键字。