simplebeginnerquestion(**args)

William Tanksley wtanksle at dolphin.openprojects.net
Sun Jul 25 21:48:38 EDT 1999


On Sun, 25 Jul 1999 12:47:46 GMT, Brian wrote:

>I'm trying to figure out how to use the (**args) thing. I can manage
>to pass an arbitrary # of arguments to a function with (*stuff) as a
>tuple, but if I use (**stuff)  to do it like this:

>myfunc(**stuff):
> for i in stuff.keys():
>  print i 

This is 100% right (whoops, I just noticed the missing 'def').

>myfunc(1,2,3,4)

Whoops!  This is wrong.  Why?  Because you're calling a **argument as
though it were a *argument.  Watch this:

def myfunc(*stuff, **more):
  print "stuff ", stuff
  print "more ", more

You see?  The language has to fill some arguments into the tuple stuff,
and other arguments into the dictionary more.  Any arguments given by
themselves will be placed in order into stuff; arguments assigned to names
will be placed in the more dictionary.

>>> myfunc(1,2,3,4)
stuff (1,2,3,4)
more {}

>>> myfunc(x=3, duh="no")
stuff ()
more {x:3, duh:'no'}

>the interpreter raises an error saying it expected..0 arguments and
>got..4	if I use (*stuff) (without .keys()...) it works. 
>Shouldn't (**stuff) cause a dictionary filled with myfunc(stuff here)
>to happen and the 'print i' print up the keys?
>What I don't get is why does it expect..0 arguments?Why doesn't it
>realize that there is a ** there?

-- 
-William "Billy" Tanksley




More information about the Python-list mailing list