function call - default value & collecting arguments

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Wed Apr 2 18:55:19 EDT 2008


On 2 avr, 22:32, "Primoz Skale" <primoz.skale.li... at gmail.com> wrote:
> >> I also understand (fairly) how to collect arguments. For example, let's
> >> define another function:
>
> >> def f(*a):
> >>    print a
>
> > This means that f takes any number of optional positional arguments.
> > If nothing is passed, within f, 'a' will be an empty tuple. Note that
> > this is *not* the usual way to define a function taking multiple
> > (mandatory) arguments.
>
> M. Lutz in "Learning Python" had defined it this way. What is the *usual*
> way in this case?

You mean : "what's the usual way to define a function taking multiple
*mandatory* arguments" ? I'd think it's explained in your book ???

def f(a, b, c):
  print a, b, c

But this is such a cs101 point that we're surely misunderstanding each
other here.

>
> > or (slightly more involved, and certainly overkill):
>
> > def with_default_args(default):
> >    def decorator(func):
> >        def wrapper(*args):
> >            if not args:
> >                args = default
> >            return func(*args)
> >        return wrapper
> >    return decorator
>
> > @with_default_args((0,))
> > def f(*a):
> >    print a[0]
>
> Now, this is interesting. Thanks! :)

Dont take this as a "recommanded" solution to your problem - it was
(mostly) to be exhaustive (and a bit on the "showing off" side too to
be honest).




More information about the Python-list mailing list