[Tutor] (*args, **kwargs)

Etienne Robillard granted14 at yahoo.com
Fri Aug 4 16:38:07 CEST 2006


--- Matt Williams <matthew.williams at cancer.org.uk>
wrote:

> Dear All,
> 
> I have learnt to do bits of python, but one of the
> things I cannot get 
> my head around is the *args, **kwargs syntax.
> I have tried reading stuff on the web, and I have a
> copy of the python 
> cookbook (which uses it as a recipe early on) but I
> still don't 
> understand it.
> 
> Please could someone explain _very_ slowly?

Here's how I've learned it so far:

First, there's a convention for 'passing lists as
arguments', which is simply to use 'kw' (I prefer that
one) or 'kwargs' with a '*' in front of them.

But first you need to initialize your data structure
properly, thus:

kw = {} # a empty dictionnary

Then you may want to add stuff inside that kw object:

kw['foo'] = "bar"

Now consider the following data structure:
class Foo:
      def __init__(self, **kw):
          # do something with kw objects here.
          pass

Then here's how to pass the whole kw object as a
argument list:
Foo(**kw) 

HTH,

Etienne

> Apologies for the gross stupidity,

P.S - There's no such thing here, sorry. :-)
 
> Matt
> 
> 
> -- 
> http://acl.icnet.uk/~mw
> http://adhominem.blogsome.com/
> +44 (0)7834 899570


--
Etienne Robillard <erob at cpan.org>
JID: incidah AT njs.netlab.cz
YMS/MSN: granted14 AT yahoo.com
TEL: +1  514.962.7703
URL: http://www.assembla.com/wiki/show/stn-portfolio/

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Tutor mailing list