pythonzsi

what does double star followed by variable name mean in python?


Possible Duplicate:
What does ** (double star) and * (star) do for python parameters?

I am reading some code that been generated by ZSI for python. There is a line like this

def verifyVehicle(self, request, **kw): ....

I want to know what does this **kw neams. Is this neam a dictionary type? Thanks


Solution

  • It refers to all keyword arguments passed to the function that aren't in the method definition. For example:

    >>> def foo(arg, **kwargs):
    ...     print kwargs
    ... 
    >>> foo('a', b="2", c="3", bar="bar")
    {'c': '3', 'b': '2', 'bar': 'bar'}
    

    It is similar to just using one asterisk, which refers to all non-keyword arguments:

    >>> def bar(arg, *args):
    ...     print args
    ... 
    >>> bar(1, 2, 3, 'a', 'b')
    (2, 3, 'a', 'b')
    

    You can combine these(and people often do)

    >>> def foobar(*args, **kwargs):
    ...     print args
    ...     print kwargs
    ... 
    >>> foobar(1, 2, a='3', spam='eggs')
    (1, 2)
    {'a': '3', 'spam': 'eggs'}