转义保留关键字Python

2024-09-29 19:20:51 发布

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

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

invoice = invoiceType(
    customFieldList = customFieldList,
    entity = entityRecord,
    subsidiary = subRecord,
    department = departmentRecord,
    location = locationRecord,
    class = classRecord
)

我不能选择将最后一个参数从say'class'改为'class'或其他东西,因为这正是NetSuite期望参数被调用的地方。我可以在python中使用其他方法吗?有没有一种方法可以在将其作为参数传递时进行转义?在


Tags: 方法代码参数原因发票invoice关键字class
1条回答
网友
1楼 · 发布于 2024-09-29 19:20:51

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

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

它所做的是用一个名为'class'的键创建一个字典,然后将字典展开为一个参数,这样就不必指定字面的class关键字。在

相关问题 更多 >

    热门问题