473,382 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Function params with **? what do these mean?

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

Mar 20 '06 #1
11 1267
On 20 Mar 2006 12:46:43 -0800 in comp.lang.python, "J Rice"
<ri**********@gmail.com> wrote:
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

--
Change is inevitable, progress is not.
Mar 20 '06 #2
in the parameter list, **param gets a dict of arguments that dont
correspond to somthing in the formal parameter list.

More & examples in the python docs:
http://docs.python.org/tut/node6.htm...00000000000000

--
Jordan T. Greenberg

Mar 20 '06 #3
J Rice wrote:
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:
def foo(*args):
return sum(args) foo(1,2,3)
6

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

or
def bar(**kwargs):
for key, value in kwargs.items():
print "key=%s, value=%s" % (key, value)
return bar(a=1, b=2, c="this is a test")

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
Mar 20 '06 #4
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.

Mar 20 '06 #5
In article <r5********************************@4ax.com>,
Dave Hansen <id**@hotmail.com> wrote:
On 20 Mar 2006 12:46:43 -0800 in comp.lang.python, "J Rice"
<ri**********@gmail.com> wrote:

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


Personally, I think it's a Good Idea to stick with the semi-standard
names of *args and **kwargs to make searching easier...
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"19. A language that doesn't affect the way you think about programming,
is not worth knowing." --Alan Perlis
Mar 20 '06 #6
On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
aa**@pythoncraft.com (Aahz) wrote:
In article <r5********************************@4ax.com>,
Dave Hansen <id**@hotmail.com> wrote:

[...]
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...


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

--
Change is inevitable, progress is not.
Mar 21 '06 #7
Dave Hansen wrote:
On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
aa**@pythoncraft.com (Aahz) wrote:
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).


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

Fortunately, **kw is also semi-standard.

--Ben

Mar 21 '06 #8
On Mon, 20 Mar 2006, Ben Cartwright wrote:
Dave Hansen wrote:
On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
aa**@pythoncraft.com (Aahz) wrote:
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).


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. ;)
Mar 21 '06 #9
Aahz wrote:
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

Mar 21 '06 #10
J Rice wrote:
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.

--
-Scott David Daniels
sc***********@acm.org
Mar 23 '06 #11
Scott David Daniels <sc***********@acm.org> writes:
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.

--
\ "Ice Water? Get some onions - that'll make your eyes water!" |
`\ -- Groucho Marx |
_o__) |
Ben Finney

Mar 23 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Stephen Boulet | last post by:
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
4
by: Alex Vinokur | last post by:
Hi, I need something like "function inheritance". typedef void (*func_type1) (int); typedef int (*func_type2) (double); typedef char (*func_type3) (short, int); .... I need a vector...
7
by: Andrej Prsa | last post by:
Hello, everyone! When a const int * argument is passed to a function, i.e. int f (const int *var) { printf ("%d\n", *var); } int main ()
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
1
by: craigkenisston | last post by:
Hi, I'm trying to create a generic function to call stored procedures in a SQL Server database. I'm passing the params, values and the direction of the params in arrays. The function is...
6
by: daveyand | last post by:
Hey Guys, I've stumped. I created a function that does various things to select boxes. Namely Get All selected indexes, populate array with these values
5
by: reycri | last post by:
Hi, I need to be able to do this: var func = new Function("var me = <selfRef>; alert(me.params);"); func.params = "This is a test parameter"; window.setTimeout(func, 500); Basically, I...
4
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
3
by: lawrence k | last post by:
One thing I like about Ruby is the use of symbols when passing function parameters to a function. One thing I dislike about PHP is that if a function has 4 optional parameters but you want to do...
4
by: lawrence k | last post by:
I'm working in PHP 4. Even if a parameter is mandatory, I nearly always give it a default value of "false". Then I print an error message if someone who is calling my function forgot the mandatory...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.