Google Groups Home
Help | Sign in
*args and **kwargs
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
JonathanB  
View profile
 More options Jun 5 2007, 10:27 pm
Newsgroups: comp.lang.python
From: JonathanB <doulo...@gmail.com>
Date: Tue, 05 Jun 2007 05:27:58 -0700
Local: Tues, Jun 5 2007 10:27 pm
Subject: *args and **kwargs
Ok, this is probably definitely a newbie question, but I have looked
all over the Python library reference material and tutorials which I
can find online and I cannot find a clear definition of what these are
and more importantly how to use them. From what I can tell from their
use in the examples I've seen, they are for passing a variable number
of arguments to a function (which I need to do in a program I am
working on). But how do you use them? Is there a fixed order in which
the arguments within *arg or **kwarg should be passed or will be
called within a function? I realize this probably involves a long-
winded answer to a very simple and common programming problem, so if
someone has a link to TFM, I'll gladly go RTFM. I just can't find it.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Diez B. Roggisch  
View profile
 More options Jun 5 2007, 10:31 pm
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Tue, 05 Jun 2007 14:31:24 +0200
Local: Tues, Jun 5 2007 10:31 pm
Subject: Re: *args and **kwargs

JonathanB wrote:
> Ok, this is probably definitely a newbie question, but I have looked
> all over the Python library reference material and tutorials which I
> can find online and I cannot find a clear definition of what these are
> and more importantly how to use them. From what I can tell from their
> use in the examples I've seen, they are for passing a variable number
> of arguments to a function (which I need to do in a program I am
> working on). But how do you use them? Is there a fixed order in which
> the arguments within *arg or **kwarg should be passed or will be
> called within a function? I realize this probably involves a long-
> winded answer to a very simple and common programming problem, so if
> someone has a link to TFM, I'll gladly go RTFM. I just can't find it.

That's because it's in the language reference, not in the library reference.

http://docs.python.org/ref/calls.html

Diez


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Jollans  
View profile
 More options Jun 5 2007, 10:37 pm
Newsgroups: comp.lang.python
From: "Thomas Jollans" <tho...@jollans.NOSPAM.com>
Date: Tue, 5 Jun 2007 13:37:22 +0100
Local: Tues, Jun 5 2007 10:37 pm
Subject: Re: *args and **kwargs
"JonathanB" <doulo...@gmail.com> wrote in message

news:1181046478.824231.117300@q75g2000hsh.googlegroups.com...

> Ok, this is probably definitely a newbie question, but I have looked
> all over the Python library reference material and tutorials which I
> can find online and I cannot find a clear definition of what these are
> and more importantly how to use them. From what I can tell from their
> use in the examples I've seen, they are for passing a variable number
> of arguments to a function (which I need to do in a program I am
> working on). But how do you use them? Is there a fixed order in which
> the arguments within *arg or **kwarg should be passed or will be
> called within a function? I realize this probably involves a long-
> winded answer to a very simple and common programming problem, so if
> someone has a link to TFM, I'll gladly go RTFM. I just can't find it.

I hope this example code will help you understand:

>>> def a(*stuff):

 print repr(stuff)

>>> def b(**stuff):

 print repr(stuff)

>>> def c(*args, **kwargs):

 print 'args', repr(args)
 print 'kwargs', repr(kwargs)

>>> a(1,2,3)
(1, 2, 3)
>>> b(hello='world', lingo='python')

{'hello': 'world', 'lingo': 'python'}
>>> c(13,14,thenext=16,afterthat=17)

args (13, 14)
kwargs {'afterthat': 17, 'thenext': 16}
>>> args = [1,2,3,4]
>>> kwargs = {'no-way': 23, 'yet-anotherInvalid.name': 24}
>>> c(*args, **kwargs)

args (1, 2, 3, 4)
kwargs {'no-way': 23, 'yet-anotherInvalid.name': 24}


(sorry for the messed-up formatting)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wildemar Wildenburger  
View profile
(1 user)  More options Jun 5 2007, 10:42 pm
Newsgroups: comp.lang.python
From: Wildemar Wildenburger <wilde...@freakmail.de>
Date: Tue, 05 Jun 2007 14:42:04 +0200
Local: Tues, Jun 5 2007 10:42 pm
Subject: Re: *args and **kwargs
JonathanB wrote:
> Ok, this is probably definitely a newbie question, but I have looked
> all over the Python library reference material and tutorials which I
> can find online and I cannot find a clear definition of what these are
> and more importantly how to use them. From what I can tell from their
> use in the examples I've seen, they are for passing a variable number
> of arguments to a function (which I need to do in a program I am
> working on). But how do you use them? Is there a fixed order in which
> the arguments within *arg or **kwarg should be passed or will be
> called within a function? I realize this probably involves a long-
> winded answer to a very simple and common programming problem, so if
> someone has a link to TFM, I'll gladly go RTFM. I just can't find it.

http://www.python.org/doc/faq/programming/#how-can-i-pass-optional-or...
(the first hit when you search python.org for *args and **kwargs)

Basically 'args' is a tuple with all the positional arguments, kwargs is
a dictionary with all the named arguments.
Likewise you can pass a tuple to a function like func(*tuple), or a dict
like func(**dictionary) or both, where the zuple has to come first.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
JonathanB  
View profile
 More options Jun 5 2007, 10:46 pm
Newsgroups: comp.lang.python
From: JonathanB <doulo...@gmail.com>
Date: Tue, 05 Jun 2007 05:46:02 -0700
Local: Tues, Jun 5 2007 10:46 pm
Subject: Re: *args and **kwargs
> I hope this example code will help you understand:
>>Code Snipped<<

OOH!! That makes perfect sense, thanks!, *args are passed as a turple,
**kwargs are passed as a dictionary. That means **kwargs is probably
what I want.

JonathanB


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
BartlebyScrivener  
View profile
 More options Jun 6 2007, 1:17 am
Newsgroups: comp.lang.python
From: BartlebyScrivener <bscrivene...@gmail.com>
Date: Tue, 05 Jun 2007 15:17:17 -0000
Local: Wed, Jun 6 2007 1:17 am
Subject: Re: *args and **kwargs
On Jun 5, 7:31 am, "Diez B. Roggisch" <d...@nospam.web.de> wrote:

> JonathanB wrote:
> > Ok, this is probably definitely a newbie question, but I have looked
> > all over the Python library reference material and tutorials which I
> > can find online and I cannot find a clear definition of what these are
> > and more importantly how to use them.

Also, well maybe op did not check THE tutorial. Or found explanation
too terse. But it's there.

http://docs.python.org/tut/node6.html#SECTION006600000000000000000

rd


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google