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

Diez B. Roggisch deets at nospam.web.de
Wed Jan 31 12:57:56 CET 2007


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



More information about the Python-list mailing list