Message from discussion
*args and **kwargs
Path: g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.albasani.net!feed.cnntp.org!news.cnntp.org!not-for-mail
From: "Thomas Jollans" <tho...@jollans.NOSPAM.com>
Newsgroups: comp.lang.python
References: <1181046478.824231.117300@q75g2000hsh.googlegroups.com>
Subject: Re: *args and **kwargs
Date: Tue, 5 Jun 2007 13:37:22 +0100
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
X-RFC2646: Format=Flowed; Original
Lines: 47
Message-ID: <46655ac1$0$2892$6e1ede2f@read.cnntp.org>
Organization: CNNTP
NNTP-Posting-Host: 0daabf18.read.cnntp.org
X-Trace: DXC=>FGgE@`gg9CT4_6BS`k^@IWoT\PAgXa?AKQcNg`?eU^G7Bf=h81CgiOPHUL<2?C2\IJXRSOefeV2JYT8ghIfgN2D
X-Complaints-To: abuse@cnntp.org
"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)