Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

zsi

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

like image 546
ACZINT Avatar asked Apr 18 '12 01:04

ACZINT


People also ask

What does it mean by double * in Python?

In a function definition, the double asterisk is also known **kwargs. They used to pass a keyword, variable-length argument dictionary to a function. The two asterisks (**) are the important element here, as the word kwargs is conventionally used, though not enforced by the language.

What does * indicate in Python?

The asterisk (star) operator is used in Python with more than one meaning attached to it. For numeric data types, * is used as multiplication operator >>> a=10;b=20 >>> a*b 200 >>> a=1.5; b=2.5; >>> a*b 3.75 >>> a=2+3j; b=3+2j >>> a*b 13j.

What does variable double star mean?

After you have found each of the stars listed here, move onto the Astronomical League's Double Star Club checklist of targets. Variable stars. Like double stars, variable stars are not as adversely affected by light pollution as some other sky targets are.

What does * mean in Python argument?

Python has *args which allow us to pass the variable number of non keyword arguments to function. In the function, we should use an asterisk * before the parameter name to pass variable length arguments.

What does the double star operator mean in Python?

What does the Double Star operator mean in Python? For numeric data types double asterisk (**) is defined as exponentiation operator In a function definition, argument with double asterisks as prefix helps in sending multiple keyword arguments to it from calling environment

What does double asterisk mean in Python?

For numeric data types double asterisk (**) is defined as exponentiation operator In a function definition, argument with double asterisks as prefix helps in sending multiple keyword arguments to it from calling environment What does the Star operator mean in Python?

What is the use of stars in python function arguments?

In Python function, an argument with single asterisk (star) prefixed to it helps in receiving variable number of argument from calling environment. Argument with double asterisks (stars) is used in function definition when variable number of keyword arguments have to be passed to a function.

What is the use of asterisk (star) in Python?

In Python function, an argument with single asterisk (star) prefixed to it helps in receiving variable number of argument from calling environment Argument with double asterisks (stars) is used in function definition when variable number of keyword arguments have to be passed to a function


1 Answers

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'}
like image 70
Nolen Royalty Avatar answered Nov 15 '22 20:11

Nolen Royalty