pythonmollie

Parameter naming conflict with Python


While implementing a third-party API (mollie), it seems they've named one of the parameters to support pagination "from" which conflicts with the built in python from.

Is there a way for me to use this properly? Am I not passing the parameters correctly? Note: they're written as **params.

The only parameters it supports are: from and limit.

from mollie.api.client import Client

c = Client().set_api_key(**KEY**)

c.subscriptions.list() # This works
c.subscriptions.list(limit=5) # This works
c.subscriptions.list(from="customer_code")

Gives:

  File "main.py", line 7
    c.subscriptions.list(from="customer_code")
                         ^
SyntaxError: invalid syntax

Solution

  • Assuming that the Client is defined something like:

    def list(**params):
        # some stuff with params
        print(params.get('from'))
        print(params.get('limit'))
    

    Then indeed calling list(from=5) will give a syntax error. But as the function packs all arguments to a kwargs dict and treats them as string keys, we can do the same thing on the other side - unpack a dict to the function arguments:

    list(**{'from': 5, 'limit': 10})
    

    Will indeed print:

    5
    10