gregV
gregV

Reputation: 1097

formal function arguments followed by **kwargs in Function Calls

I understand the function arguments must have the following ordering hierarchy:

  1. Formal positional arguments
  2. *args
  3. Keyword arguments
  4. **kwargs

I'm getting this error:

SyntaxError: keyword can't be an expression

in the below:

def dfDiff(old, new, **kwargs):
    # default dict of optional function arguments
    d = {'city': 'Austin',
         'capital': True,
         'indx' : 5}
    # if optional args are provided 
    if kwargs is not None:
        # find the common keys
        k_passed = kwargs.keys() & d.keys()
        # change the default value
        for k in k_passed:
            d[k] = kwargs[k]    


test_ = dfDiff(1, 2, 'city' = 'Albany')

did I pass **kwargs incorrectly or there is some other issue?

Upvotes: 1

Views: 51

Answers (2)

Michael Bianconi
Michael Bianconi

Reputation: 5232

city='Albany', don't wrap city in quotation marks.

Upvotes: 3

J Arun Mani
J Arun Mani

Reputation: 620

As @MichaelBianconi said in comments, while giving keyword-arguments, you need not enclosing them as strings. Just treat them like variables. So correct code is:

test_ = dfDiff(1, 2, city = 'Albany')

As per guidelines, don't have space between keyword, equal to and value.

test_ = dfDiff(1, 2, city='Albany')

Upvotes: 1

Related Questions