Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

**kwargs parameter name has a "." in it creating an error

I am trying to pass an argument into a function with **kwargs
allas some of the argument names contain a . and I get an error as code editor expect argument name and don’t recognize it, likely because of the "."

def get_active_offers(**kwargs):
    params = {}
    for args in kwargs:
        params[args]=kwargs.values() 
    response = api_request(params)
    return response


test = get_active_offers(BasicFilters.PriceFrom="1")

is there a way around it (something like an f string for arguments?)

for the moment my solution has been to pass the name of the argument and the value as a list and naming it with a simpler name but I am trying to learn the proper way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def get_active_offers(**kwargs):
    params = {}
    for args in kwargs.values():
        params[args[0]]=args[1] 
    response = api_request(params)
    return response


test = get_active_offers(argument = ["BasicFilters.PriceFrom","1"])

>Solution :

As one of the comments said, you can pass the argument as a dict:

def get_active_offers(**kwargs):
    params = {}
    for args in kwargs:
        params[args]=kwargs.values() 
    response = api_request(params)
    return response


test = get_active_offers(**{"BasicFilters.PriceFrom":"1"})

The above syntax of using ** with the argument is called "dictionary unpacking"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading