So I am trying out the new python code for the google app engine search library and I came across a weird syntax. This was:
cls_createDocument(**params)where params was a dictionary.
The function this refers to is:
@classmethod def _createDocument( cls, pid=None, category=None, name=None, description=None, category_name=None, price=None, **params)My questions is, what does the **params signify and what does that do to the object?
Thanks! Jon
02 Answers
Consider a function with default arguments:
def func(foo=3): print(foo)The structure of the arguments is (in principle) very similar to a dictionary. The function foo has (essentially) a dictionary of default arguments (in this case {'foo':3}). Now, lets say that you don't want to use the keyword in the function call, but you want to use a dictionary instead -- then you can call foo as:
d = {"foo":8}
func(**d)This allows you to dynamically change what arguments you are passing to the function func.
This become a little more interesting if you try the following:
d = {"foo":8, "bar":12}
func(**d)This doesn't work (it is equivalent to foo(foo=8, bar=12), but since bar isn't a valid argument, it fails).
You can get around that problem by giving those extra arguments a place to go inside the definition of foo.
def func( foo=3, **kwargs ): print(foo,kwargs)Now, try:
d = {"foo":8, "bar":12}
func(**d) #prints (8, {'bar':12})All the extra keyword arguments go into the kwargs dictionary inside the function.
This can also be called as:
func(foo=8, bar=12)with the same result.
This is often useful if funcA calls funcB and you want funcA to accept all of the keywords of funcB (plus a few extra) which is a very common thing when dealing with classes and inheritance:
def funcA(newkey=None,**kwargs): funcB(**kwargs)Finally, here is a link to the documentation
2The **params parameter represents all the keyword arguments passed to the function as a dictionary.