what does double star followed by variable name mean in python? [duplicate]
Asked Answered
D

1

8

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

Dugald answered 18/4, 2012 at 1:48 Comment(0)
H
21

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'}
Hypogeum answered 18/4, 2012 at 1:50 Comment(1)
That explains everything! ThanksDugald

© 2022 - 2024 — McMap. All rights reserved.