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
Consider a function with default arguments:
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 callfooas: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:
This doesn’t work (it is equivalent to
foo(foo=8, bar=12), but sincebarisn’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.Now, try:
All the extra keyword arguments go into the kwargs dictionary inside the function.
This can also be called as:
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:
Finally, here is a link to the documentation