Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a `kwargs` list?

From an external file I generate the following dictionary:

mydict = { 'foo' : 123, 'bar' : 456 }

Given a function that takes a **kwargs argument, how can generate the keyword-args from that dictionary?

like image 916
Paul Manta Avatar asked Sep 15 '11 20:09

Paul Manta


People also ask

How do I pass a list to Kwargs?

The ** unpacking operator can be used to pass kwargs from one function to another function's kwargs. Consider this code: (newlines don't seem to be allowed in comments) def a(**kw): print(kw) , and def b(**kw): a(kw) .

How do I unpack Kwargs?

Unpacking kwargs and dictionaries You cannot directly send a dictionary as a parameter to a function accepting kwargs. The dictionary must be unpacked so that the function may make use of its elements. This is done by unpacking the dictionary, by placing ** before the dictionary name as you pass it into the function.

What is a Kwargs?

kwargs is variable name used for keyword arguments, another variable name can be used. The important part is that it's a dictionary and it's unpacked with the double asterisk operator ** . Other iterables are unpacked with the single asterisk operator *

What is Kwargs get in Python?

In Python, keyword arguments(kwargs) are values that are accessed by specific parameters in a function. A keyword argument is followed by a parameter and the assignment operator (=). In Python, keyword arguments can be passed through dictionaries.


1 Answers

def foo(**kwargs):
    pass

foo(**{ 'foo' : 123, 'bar' : 456 })
like image 50
Falmarri Avatar answered Nov 04 '22 12:11

Falmarri