Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Function params with **? what do these mean?

Reply
Thread Tools

Function params with **? what do these mean?

 
 
J Rice
Guest
Posts: n/a
 
      03-20-2006
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

 
Reply With Quote
 
 
 
 
Dave Hansen
Guest
Posts: n/a
 
      03-20-2006
On 20 Mar 2006 12:46:43 -0800 in comp.lang.python, "J Rice"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Jordan Greenberg
Guest
Posts: n/a
 
      03-20-2006
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

 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      03-20-2006
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


 
Reply With Quote
 
J Rice
Guest
Posts: n/a
 
      03-20-2006
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.

 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      03-20-2006
In article <(E-Mail Removed)>,
Dave Hansen <(E-Mail Removed)> wrote:
>On 20 Mar 2006 12:46:43 -0800 in comp.lang.python, "J Rice"
><(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"19. A language that doesn't affect the way you think about programming,
is not worth knowing." --Alan Perlis
 
Reply With Quote
 
Dave Hansen
Guest
Posts: n/a
 
      03-21-2006
On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Aahz) wrote:

>In article <(E-Mail Removed)>,
>Dave Hansen <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ben Cartwright
Guest
Posts: n/a
 
      03-21-2006
Dave Hansen wrote:
> On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
> (E-Mail Removed) (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

 
Reply With Quote
 
Dave Benjamin
Guest
Posts: n/a
 
      03-21-2006
On Mon, 20 Mar 2006, Ben Cartwright wrote:

> Dave Hansen wrote:
>> On 20 Mar 2006 15:45:36 -0800 in comp.lang.python,
>> (E-Mail Removed) (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.
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      03-21-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
params v.s. @params in rails? Barry Ruby 9 09-15-2005 03:12 AM
[OT] PerlTidy option to close parens on function params *only* newsbot@cox.net Perl Misc 0 05-02-2005 08:00 PM
Problem configuring wireless card (setting SSID and other params) Serhiy Smirnov Wireless Networking 2 03-07-2005 07:04 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Getting params in callback function. Stephen Kellett Python 0 03-05-2004 02:29 PM



Advertisments