Function params with **? what do these mean?

J

J Rice

I'm sorry for such a basic question, but I haven't been able to phrase
a search that gets me an answer and my books are totally silent on
this. I have seen a number of python function defs that take
parameters of the form (**param1). Looks like a pointer... but my
books on python (basic as they are) don't make a mention. What is
this?

Jeff
 
D

Dave Hansen

I'm sorry for such a basic question, but I haven't been able to phrase
a search that gets me an answer and my books are totally silent on
this. I have seen a number of python function defs that take
parameters of the form (**param1). Looks like a pointer... but my
books on python (basic as they are) don't make a mention. What is
this?

It's a way of accepting a varying number of named arguments. In the
function, the parameter becomes a dictionary with parameter names as
the keys corresponding to the passed parameter values.

It's harder to explain than understand. Try playing with the
following function in the python interpreter:

def test(a,b='b', *c, **d):
print a,b,c,d

A couple suggestions for tests:

test(1,2,3,4)
test(a=1,b=2,c=3,d=4)
test(2,4,6,8,10,12,ralph=23,tony=45)

See what happens. Should be mostly self-explanatory.

Regards,
-=Dave
 
L

Larry Bates

J said:
I'm sorry for such a basic question, but I haven't been able to phrase
a search that gets me an answer and my books are totally silent on
this. I have seen a number of python function defs that take
parameters of the form (**param1). Looks like a pointer... but my
books on python (basic as they are) don't make a mention. What is
this?

Jeff
There are too forms that you may be confusing. First
def foo(x,y,z):
return x+y+z
t=[1,2,3]
foo(*t)

6


This tells python to expand t and pass it as as 3
separate arguments

The second is:

6

This allows you to treat all the arguments to a function
as a list of arguments no matter how many there are.

or
key=a, value=1
key=c, value=this is a test
key=b, value=2


This allows you to access keyword arguments via a dictionary
in the function, instead of individually.


You can combine these to make very powerful functions/methods
that can handle different numbers of arguments and keyword
arguments.

def foo(*args, **kwargs):
....


Larry Bates
 
J

J Rice

Wow, this is incredibly useful! I can understand why an introductory
book wouldn't make use of them, but I am really glad to know about
them. I can think of a bunch of ways to simply some code I have using
this.
 
A

Aahz

It's a way of accepting a varying number of named arguments. In the
function, the parameter becomes a dictionary with parameter names as
the keys corresponding to the passed parameter values.

It's harder to explain than understand. Try playing with the
following function in the python interpreter:

def test(a,b='b', *c, **d):
print a,b,c,d

Personally, I think it's a Good Idea to stick with the semi-standard
names of *args and **kwargs to make searching easier...
 
D

Dave Hansen

On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
Personally, I think it's a Good Idea to stick with the semi-standard
names of *args and **kwargs to make searching easier...

Agreed (though "kwargs" kinda makes my skin crawl). I don't use these
features often in my code, but when I do, I follow the convention. The
example was just for illustrative purposes, and the names chosen for
easy typing.

It is important to note that using "args" and "kwargs" is a convention
rather than a requirement, analogous to "self". You can use different
identifiers, but future maintainers of your code will be annoyed.

But it won't affect the operation of the code. I found the test case
"test(a=1,b=2,c=3,d=4)" to be most edifying.

Regards,
-=Dave
 
B

Ben Cartwright

Dave said:
On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,


Agreed (though "kwargs" kinda makes my skin crawl).

Coincidentally, "kwargs" is the sound my cat makes when coughing up a
hairball.

Fortunately, **kw is also semi-standard.

--Ben
 
D

Dave Benjamin

Coincidentally, "kwargs" is the sound my cat makes when coughing up a
hairball.

Fortunately, **kw is also semi-standard.

I prefer the semi-standard **kwds, myself. ;)
 
C

Carl Banks

Aahz said:
Personally, I think it's a Good Idea to stick with the semi-standard
names of *args and **kwargs to make searching easier...

I usually do stick to these names (since the I usually only use them
when forwarding arguments to another function, where such names are a
pretty good description), but I can't think of any particular reason to
search for all occurrences of them.

Carl Banks
 
S

Scott David Daniels

J said:
I'm sorry for such a basic question, but I haven't been able to phrase
a search that gets me an answer and my books are totally silent on
this. I have seen a number of python function defs that take
parameters of the form (**param1). Looks like a pointer... but my
books on python (basic as they are) don't make a mention. What is
this?

At the risk of being thought of as beating a dead horse, this was a
_great_ way to ask this question. Others may note that nobody told
you you were abusing the newsgroup to do your work for you; you told
us how you tried to answer the question for yourself.

So, anyhow, thanks for taking the time to write your question properly.
 
B

Ben Finney

Scott David Daniels said:
At the risk of being thought of as beating a dead horse, this was a
_great_ way to ask this question. [...]
So, anyhow, thanks for taking the time to write your question properly.

Take that risk, please. There's enough lambasting of (and probably
much more private grumbling over) poor delivery of questions, but not
enough praise when *good* form is followed.

The latter does much more to show what we consider appropriate.
 

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

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top