Re: another newbie question: why should you use "*args" ?



stef wrote:


why should I use *args,
as in my ignorance,
making use of a list (or tupple) works just as well,
and is more flexible in it's calling.
So the simple conclusion might be: never use "*args",
or am I overlooking something ?

Yup. For example decorators, that wrap functions. If you do that, you want
to return a function that captures all passed arguments, does something,
and then invokes the original function with the original arguments. Like
this:


def logging_decorator(f):
def _f(*args, **kwargs):
print "calling %r with (%r, %r)", % (f, args, kwargs)
return f(*args, **kwargs)

return _f


@logging_decorator
def im_just_a_function(some, argument, and, other=None):
pass




Diez
.



Relevant Pages

  • Re: Towers of Hanoi Problem
    ... > perhaps with different names and a slighlty different print statement. ... > I did notice last night that the output from my original function was ... > the args to hanoi mixed up. ... nice little sanity checker you have there. ...
    (comp.lang.lisp)
  • Re: Test for structure
    ... At the end of his last post, Steve Bethard wrote: ... > in the original function though. ... possible to use the *args idea to greatly simplify the proposed ... def aslist: ...
    (comp.lang.python)