Mailing List Archive

simplebeginnerquestion(**args)
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

myfunc(1,2,3,4)

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?
simplebeginnerquestion(**args) [ In reply to ]
Brian,

When you're going to use "named" arguments, such as
myfunc(a=1, b=2, c=3, d=4)
then it's appropriate to use
def myfunc(**kw): pass
but when you're using unnamed arguments, such as
myfunc(1, 2, 3, 4)
you want to use
def myfunc(*args): pass

This makes sense, because for unnamed arguments the position is important
but there's no sensible key to give them if they were placed in a
dictionary---so instead, you get them as a tuple with "*args". But if the
arguments are named, obviously they need to go into a dictionary.

Jeff
--
\/ http://www.infidels.org/ Jeff Epler jepler@inetnebr.com
"Pay no attention to the man behind the curtain."
-- Karl, as he stepped behind the computer to reboot it, during a FAT
simplebeginnerquestion(**args) [ In reply to ]
Python 1.5.1 (#1, Jan 31 1999, 18:55:21) [GCC 2.8.1] on irix6
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def keyword_args_demo(**args): print args
...
>>> keyword_args_demo(first_argument=1,second_argument=2)
{'first_argument': 1, 'second_argument': 2}
>>>

You have to explicitly say what each of the keyword arguments are
called. If you don't want to name each argument, use a single asterix:

>>> def arbitrary_args_demo(*args): print args
... # Note the single asterix
...
>>> arbitrary_args_demo(1,2,3,4)
(1, 2, 3, 4)

Alex.

--
I have tried to write the best I can; sometimes I have good luck and can
write better than I can. --Ernest Hemingway
simplebeginnerquestion(**args) [ In reply to ]
using a bogus mail address, "optional (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
>
> myfunc(1,2,3,4)
>
> 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?

interesting. what did you expect the "keys" to
be in this case?

> What I don't get is why does it expect..0 arguments? Why doesn't it
> realize that there is a ** there?

reading the manual might help:

If the form ``**identifier'' is present, it is initialized
to a new dictionary receiving any excess keyword
arguments.

if you don't pass the function any keyword arguments
at all, there are obviously no excess keyword arguments
to put in that dictionary...

</F>
simplebeginnerquestion(**args) [ In reply to ]
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