Search code examples
pythonkeyword-argument

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?


Solution

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