I'm trying to generate a dict containing the names and the value of the non-None
parameters current call. This very much feels like something that should be a built-in, but can't find anything that quite does it in the Python documentation.
For example, when
def foo(limit=None, offset=None, lower_bound=None, upper_bound=None):
# code to generate dict of non-`None` named arguments (see below)
... # other things happen later
is called with the parameters foo(limit=5, lower_bound=100)
, I need the following dict: {limit=5, lower_bound=100}
.
Is there something that conveniently does that for me?
So far, I have looked into the following, all of which seem flawed:
def foo(limit=None, offset=None, lower_bound=None, upper_bound=None):
keys = ('limit', 'offset', 'lower_bound', 'upper_bound')
values = (limit, offset, lower_bound, upper_bound)
magic_dict = {k: v for k, v in zip(keys, values) if v is not None}
locals()
— link — which would need everything removed that shouldn't make it into the dictionary (i.e. the keys with the value of None
). This also seems potentially error prone if variables definitions get added above the magic dictionary code, which would also need to be removed.def foo(limit=None, offset=None, lower_bound=None, upper_bound=None):
magic_dict = {k: v for k, v in locals().items() if v is not None}
inspect
's — link — signature
and bind
, which still requires would a list of the parametersdef foo(limit=None, offset=None, lower_bound=None, upper_bound=None):
sig = signature(foo)
sig_args = sig.bind(limit, offset, lower_bound, upper_bound).arguments
magic_dict = {k: v for k, v in sig_args.items() if v is not None}
Other thoughts included making a class to make getattr
available
It feels like I'm missing something obvious — feedback would be appreciated!
Someone suggested
What is an elegant way to select all non-None elements from parameters and place them in a python dictionary?, whose responses are primarily "move to **kwargs
". My initial reaction is that I would prefer to keep a specific list of arguments — is that bad logic by me? What is typical best practice with Python?
Passing Dictionary as kwargs “ kwargs ” stands for keyword arguments. It is used for passing advanced data objects like dictionaries to a function because in such functions one doesn't have a clue about the number of arguments, hence data passed is be dealt properly by adding “**” to the passing type.
The ** symbol is used before an argument to pass a keyword argument dictionary to a function, this syntax used to successfully run the code when we don't know how many keyword arguments will be sent to the function. u can pass the object of Foo class in func foo as a parameter.
Using zip() with dict() function The simplest and most elegant way to build a dictionary from a list of keys and values is to use the zip() function with a dictionary constructor.
Introduction to the Python **kwargs parametersWhen a function has the **kwargs parameter, it can accept a variable number of keyword arguments as a dictionary.
You can try using decorators:
def capture(func):
capture.kwds = None
def wrap(*args, **kwargs):
capture.kwds = kwargs
return func(*args, **kwargs)
return wrap
@capture
def foo(limit=None, offset=None, lower_bound=None, upper_bound=None):
return None
Now you can call:
>>> foo(limit=1, offset=2, lower_bound=3, upper_bound=4)
None
>>> capture.kwds
{'limit': 1, 'offset': 2, 'lower_bound': 3, 'upper_bound': 4}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With