Reputation: 31513
I have the following function:
def foo(**kwargs):
if not kwargs:
# No keyword arguments? It's all right. Set defaults here...
elif ('start_index' or 'end_index') in kwargs:
# Do something here...
else:
# Catch unexpected keyword arguments
raise TypeError("%r are invalid keyword arguments" % (kwargs.keys())
Question:
I want to make sure that the only valid keyword arguments are start_index
or end_index
. Anything else will raise an error, even if mixed with the valid ones. What's the cookbook recipe to make sure that only start_index
or end_index
are accepted? Yes, I'm looking for a cookbook recipe but I'm not sure how to search for it. I'm not sure if using an if-elif-else
structure is the correct way to do it either.
Upvotes: 5
Views: 3865
Reputation: 34688
In any case getting keys from a dict is as easy as using .get
e.g.
kwargs.get('WIDTH',500)
this way if it doesn't find WIDTH as a key you get 500.
Upvotes: 0
Reputation: 156148
For the sake of completeness, Here's an alternative that still uses **kwargs
.
def foo(**kwargs):
start_index = kwargs.pop('start_index', STARTINDEX_DEFAULT)
end_index = kwargs.pop('end_index', ENDINDEX_DEFAULT)
if kwargs:
# Catch unexpected keyword arguments
raise TypeError("%r are invalid keyword arguments" % (kwargs.keys())
# Do something here...
But, you shouldn't want to use this when you don't absolutely need it, use regular parameters with default values (as in Roman Bodnarchuk's answer).
Cases when you might need this is when you also want to use *args
, and need a way to distinguish the keyword arguments from arbitrarily man positional arguments. using **kwargs
this way forces the keyword arguments to be passed as keywords; A positional argument can never find its way into **kwargs
.
Another reason is so that you can really distinguish between a default and an explicit parameter which happens to be the default. None
is often used as a default value for arguments to indicate "the argument doesn't apply", but sometimes you actually need to interpret the None
as something other than a default. Checking for the presence or absence of a key in the **kwargs
dict can accurately distinguish between these cases. (An alternative is to create an instance of a subclass of object
whos sole purpose is to be the default value of a specific argument to that specific function)
Upvotes: 5
Reputation: 30943
If you really want to use **kwargs
, I'd write that like:
def foo(**kwargs):
# Define default values for all keys
args = {'start_index': 0, 'end_index': -1}
# Get the keys passed in that aren't in args
extraargs = set(kwargs) - set(args)
if extraargs:
raise TypeError("Invalid arguments: %s" % list(extraargs))
# Overwrite the default values with the passed-in values
args.update(kwargs)
# Now, do stuff with the values in args
But all of that is a complicated, slow way to duplicate built-in functionality. Don't do that unless you really need to.
Upvotes: 1
Reputation: 29717
Why do you need **kwargs
here? Just
def foo(start_index=None, end_index=None):
and Python will perform all validation for you.
Upvotes: 13