Search code examples
pythonpython-3.xnetsuitezeepsuitetalk

Escape Reserved Keywords Python


I am using ZEEP to connect to NetSuite. One of the parameters that I need to pass to NS when creating the invoice is 'class'. If I understand correctly, the reason the following line does not compile is because 'class' is a reserved keyword.

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

I don't have the option to change the last parameter from say 'class' to 'Class' or something else, since this is what NetSuite expects the parameter to be called. Are there any alternatives that I can use in python? Is there a way to escape this while passing it as a parameter?


Solution

  • You'll need to use the **{...} syntax to pass a keyword argument whose name is a reserved word.

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

    What this is doing is creating a dictionary with a key called 'class' and then unrolling the dictionary into an argument so you never have to specify the literal class keyword.