Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a parameter into kwargs during function call?

Is there a way to add key-value-pair into kwargs during the function call?

def f(**kwargs):
    print(kwargs)

# ...

pre_defined_kwargs = {'a': 1, 'b': 2}

f(**pre_defined_kwargs, c=3)

Or even change the existing arguments?

f(**pre_defined_kwargs, b=3)  # replaces the earlier b=2

These two examples don't work, as they raise error

>>> f(**pre_defined_kwargs, c=3)
SyntaxError: invalid syntax

Pointing at the comma in between the arguments

like image 695
Markus Meskanen Avatar asked Jan 11 '15 21:01

Markus Meskanen


People also ask

How do you pass things in Kwargs?

Kwargs allow you to pass keyword arguments to a function. They are used when you are not sure of the number of keyword arguments that will be passed in the function. Kwargs can be used for unpacking dictionary key, value pairs. This is done using the double asterisk notation ( ** ).

How do you add Kwargs in Python?

The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is that the double star allows us to pass through keyword arguments (and any number of them).

What is the correct way to use the Kwargs statement?

The double asterisk form of **kwargs is used to pass a keyworded, variable-length argument dictionary to a function. Again, the two asterisks ( ** ) are the important element here, as the word kwargs is conventionally used, though not enforced by the language.

What is the difference between args and Kwargs?

The term Kwargs generally represents keyword arguments, suggesting that this format uses keyword-based Python dictionaries. Let's try an example. **kwargs stands for keyword arguments. The only difference from args is that it uses keywords and returns the values in the form of a dictionary.


2 Answers

For Python versions < 3.5, you need to place the **kwargs variable keyword argument last:

f(c=3, **pre_defined_kwargs)

See the Calls expression syntax; in all forms of the grammar the "**" expression rule is placed last. In other words, using anything after the **expression syntax is a syntax error.

If you want to update the dictionary with new values, you can use the dict() callable; it can create a copy of your existing dictionary and update keys in that, provided the keys are also valid Python identifiers (start with a letter or underscore, and only contain letters, digits and underscores):

f(c=3, **dict(pre_defined_kwargs, b=42))

Here b=42 sets a new value for the 'b' key. This same syntax can be used to add keys too, of course.

You cannot use the same key both in the **expression mapping and explicitly; that'll raise a TypeError. Again, from the documentation already linked:

If the syntax **expression appears in the function call, expression must evaluate to a mapping, the contents of which are treated as additional keyword arguments. In the case of a keyword appearing in both expression and as an explicit keyword argument, a TypeError exception is raised.

Demo:

>>> def f(**kwargs):
...     print(kwargs)
... 
>>> pre_defined_kwargs = {'a': 1, 'b': 2}
>>> f(c=3, **pre_defined_kwargs)
{'a': 1, 'c': 3, 'b': 2}
>>> dict(pre_defined_kwargs, b=42)
{'a': 1, 'b': 42}
>>> f(c=3, **dict(pre_defined_kwargs, b=42))
{'a': 1, 'c': 3, 'b': 42}

This restriction has been lifted as of Python 3.5 (thanks to PEP-448 -- Additional Unpacking Generalizations; you can now freely mix the order of argument types and use multiple **mapping references in a call (using distinct mappings). Keywords still have to be unique across all arguments applied; you still can't 'override' arguments that appear more than once.

like image 53
Martijn Pieters Avatar answered Oct 19 '22 13:10

Martijn Pieters


>>> f(c=3, **pre_defined_kwargs)
{'c': 3, 'a': 1, 'b': 2}
like image 23
dm03514 Avatar answered Oct 19 '22 12:10

dm03514