Saturday 6 April 2019

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










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


Answer



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'}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...