0

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
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Spoontech
  • 133
  • 13

1 Answers1

2

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
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    I don't think the presumed definition of `list` is relevant. As long as the assumption is that keyword arguments can be used to call the function, then `**{'from': 5, ...}` is the correct solution, regardless of how the function *uses* the keyword arguments. – chepner Dec 14 '21 at 15:21
  • @chepner I guess it was just my way of convincing myself that such function can even be defined... i.e. doing `def list(from, limit):` will have the exact same syntax error... – Tomerikoo Dec 14 '21 at 15:27
  • Thanks! This is indeed the solution. And yes they're keyword arguments, my lack of experience with these is showing.. perhaps looking further into those would've helped me rather than focusing on the naming conflict. Thanks again! – Spoontech Dec 14 '21 at 15:30