Single and double asterisks preceding variables in function arguments

S

Stephen Boulet

I've run across code like "myfunction(x, *someargs, **someotherargs)",
but haven't seen documentation for this.

Can someone fill me in on what the leading * and ** do? Thanks.

Stephen
 
A

anton muhin

Stephen said:
I've run across code like "myfunction(x, *someargs, **someotherargs)",
but haven't seen documentation for this.

Can someone fill me in on what the leading * and ** do? Thanks.

Stephen

See item 7.5 in Language Reference. In short: * declaries list of
additional parameters, while ** declares map of additional parameters.
Try somehting like:

def foo(*params): print params

and

def bar(**params): print params

to find out details.

regards,
anton.
 
D

Duncan Booth

See item 7.5 in Language Reference. In short: * declaries list of
additional parameters, while ** declares map of additional parameters.
Try somehting like:

def foo(*params): print params

and

def bar(**params): print params

to find out details.

I may be wrong, but I think the OP was asking about calling functions
rather than defining them. The '*' and '**' before arguments in function
calls are described in section 5.3.4 of the language reference but, so far
as I know, isn't explained clearly in any of the more 'user level' text.
There is a brief mention in the library reference under the documention of
'apply'.

In a call of the form:

myfunction(x, *someargs, **someotherargs)

the sequence 'someargs' is used to supply a variable number of additional
positional arguments. 'someotherargs' should be a dictionary and is used to
supply additional keyword arguments.
 
S

Stephen Boulet

anton said:
See item 7.5 in Language Reference. In short: * declaries list of
additional parameters, while ** declares map of additional parameters.
Try somehting like:

def foo(*params): print params

and

def bar(**params): print params

to find out details.

regards,
anton.

<<
a={'c1':'red','c2':'blue','c3':'fusia'}
def bar(**params):
.....for k in params.keys():
.........print k, params[k]
bar(a)
Traceback (most recent call last):

Why does bar take zero arguments?

Hmm, but:

<<
def bar2(*params,**moreparams):
.....print "params are ", params,'\n'
.....for k in moreparams.keys():
.........print k, moreparams[k]
.....print "moreparams are ", moreparams

bar2(range(3),a,)
params are ([0, 1, 2], {'c3': 'fusia', 'c2': 'blue', 'c1': 'red'})

moreparams are {}
I think I've got the *params argument down (you just get the whole
argument list), but I'm still not getting the **moreparams ...

Stephen
 
P

Peter Otten

Stephen said:
<<
a={'c1':'red','c2':'blue','c3':'fusia'}
def bar(**params):
....for k in params.keys():
........print k, params[k]
bar(a)
Traceback (most recent call last):

Why does bar take zero arguments?

The error message is not as clear as it should should be. bar() takes 0
mandatory, 0 positional and an arbitrary number of keyword arguments, e.
g.:
.... print params
....{'color': 'blue', 'rgb': (0, 0, 1), 'name': 'sky'}

That's the standard way of passing optional keyword arguments, but you can
also pass them as a dictionary preceded by **:
{'color': 'blue', 'rgb': (0, 0, 1), 'name': 'sky'}

This is useful, if you want to compose the argument dict at runtime, or pass
it through to a nested function.
Hmm, but:

<<
def bar2(*params,**moreparams):
....print "params are ", params,'\n'
....for k in moreparams.keys():
........print k, moreparams[k]
....print "moreparams are ", moreparams

bar2() accepts 0 mandatory, as many positional and keyword arguments as you
like. For the expected result, try
.... print args
.... print kwd
....(1, 2, 3)
{}(1,) # tuple of optional positional args
{'color': 'blue', 'name': 'sky'} # dict of optional keyword args
And now a bogus example that shows how to compose the arguments:
.... print "x=", x
.... print "args=", args
.... print "kwd=", kwd
.... if "terminate" not in kwd:
.... kwd["terminate"] = None # avoid infinite recursion
.... bar3(x*x, *args + ("alpha",), **kwd)
....x= 2
args= ('beta',)
kwd= {'color': 'blue'}
x= 4
args= ('beta', 'alpha')
kwd= {'color': 'blue', 'terminate': None}
To further complicate the matter, a mandatory arg may also be passed as a
keyword argument:

bar3(x=2, color="blue") # will work, args is empty
bar("beta", # will not work; both "beta" and 2 would be bound to x
# and thus create a conflict
x=2, color="blue")


Peter
 
E

Eric Amick

I've run across code like "myfunction(x, *someargs, **someotherargs)",
but haven't seen documentation for this.

Can someone fill me in on what the leading * and ** do? Thanks.

Try section 4.7.2 of the tutorial.
 
A

Aahz

See item 7.5 in Language Reference. In short: * declaries list of
additional parameters, while ** declares map of additional parameters.

Actually, ``*`` forces creation of a tuple:
.... print type(bar)
....
<type 'tuple'>
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top