【问题标题】:Escape Reserved Keywords Python [duplicate]转义保留关键字Python [重复]
【发布时间】:2019-03-03 22:32:56
【问题描述】:

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

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

我没有将最后一个参数从“class”更改为“Class”或其他内容的选项,因为这是 NetSuite 期望调用的参数。我可以在 python 中使用任何替代方案吗?有没有办法在将它作为参数传递时逃避它?

【问题讨论】:

    标签: python python-3.x netsuite zeep suitetalk


    【解决方案1】:

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

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

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

    【讨论】:

    • 奇怪的是如何让论点成为类......
    • 确实; Python 包装器的 API 设计非常糟糕。
    • @chepner - Ty 用于快速响应。看起来答案奏效了。
    猜你喜欢
    • 2022-02-26
    • 1970-01-01
    • 2017-01-15
    • 2017-04-10
    • 1970-01-01
    • 2018-06-19
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多