0

I'm working with an API that has dozens of parameters in a given function:

api.update_customer(a='', b='', c='', ..., x='', y='', z='')

I'll only need to update one or a few parameters when I call it, for instance:

api.update_customer(email='[email protected]')

...but I won't know in advance which object needs updating (maybe email, maybe phone number, etc).

How can I build a wrapper around this call so I can pass in both the parameter name and its new value?

def update_customer_details(key, value):
    api.update_customer(key=value)
1
  • search for python decorators. That's what you are looking for Commented Aug 23, 2019 at 18:24

3 Answers 3

4

You don't need to write a wrapper. Just pass a dict using keyword expansion.

params = {'email': '[email protected]'}
api.update_customer(**params)
Sign up to request clarification or add additional context in comments.

Comments

1

To expand on Daniel Roseman answer for an example on how it gets used:

def test2(x=None):
    print(x)

def test(**kwarg):
    test2(**kwarg)

test(x=5)

output: 5

Comments

0

you can try:

api.update_customer(field='my_field', value='my_value', *args, **kwrgs) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.