Single and double asterisks preceding variables in function arguments
Stephen Boulet
stephendotboulet at motorola_._com
Mon Jan 26 15:47:16 EST 2004
anton muhin wrote:
> Stephen Boulet wrote:
>
>> 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.
<<
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):
File "<input>", line 1, in ?
TypeError: bar() takes exactly 0 arguments (1 given)
>>
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
More information about the Python-list
mailing list