I have a dict, which I need to pass key/values as keyword arguments.. For example..
d_args = {'kw1': 'value1', 'kw2': 'value2'} example(**d_args)
This works fine, but if there are values in the d_args dict that are not accepted by the example
function, it obviously dies.. Say, if the example function is defined as def example(kw2):
This is a problem since I don't control either the generation of the d_args
, or the example
function.. They both come from external modules, and example
only accepts some of the keyword-arguments from the dict..
Ideally I would just do
parsed_kwargs = feedparser.parse(the_url) valid_kwargs = get_valid_kwargs(parsed_kwargs, valid_for = PyRSS2Gen.RSS2) PyRSS2Gen.RSS2(**valid_kwargs)
I will probably just filter the dict, from a list of valid keyword-arguments, but I was wondering: Is there a way to programatically list the keyword arguments the a specific function takes?
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
Keyword arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names. A keyword argument is preceded by a parameter and the assignment operator, = . Keyword arguments can be likened to dictionaries in that they map a value to a keyword.
Keyword arguments are passed to functions after any required positional arguments. But the order of one keyword argument compared to another keyword argument does not matter.
Embrace keyword arguments in Python Consider using the * operator to require those arguments be specified as keyword arguments. And remember that you can accept arbitrary keyword arguments to the functions you define and pass arbitrary keyword arguments to the functions you call by using the ** operator.
A little nicer than inspecting the code object directly and working out the variables is to use the inspect module.
>>> import inspect >>> def func(a,b,c=42, *args, **kwargs): pass >>> inspect.getargspec(func) (['a', 'b', 'c'], 'args', 'kwargs', (42,))
If you want to know if its callable with a particular set of args, you need the args without a default already specified. These can be got by:
def getRequiredArgs(func): args, varargs, varkw, defaults = inspect.getargspec(func) if defaults: args = args[:-len(defaults)] return args # *args and **kwargs are not required, so ignore them.
Then a function to tell what you are missing from your particular dict is:
def missingArgs(func, argdict): return set(getRequiredArgs(func)).difference(argdict)
Similarly, to check for invalid args, use:
def invalidArgs(func, argdict): args, varargs, varkw, defaults = inspect.getargspec(func) if varkw: return set() # All accepted return set(argdict) - set(args)
And so a full test if it is callable is :
def isCallableWithArgs(func, argdict): return not missingArgs(func, argdict) and not invalidArgs(func, argdict)
(This is good only as far as python's arg parsing. Any runtime checks for invalid values in kwargs obviously can't be detected.)
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