function call - default value & collecting arguments
Primoz Skale
primoz.skale.lists at gmail.com
Thu Apr 3 04:00:19 EDT 2008
<bruno.desthuilliers at gmail.com> wrote in message
news:a3105c47-c016-49b0-8b19-841c26414b43 at s19g2000prg.googlegroups.com...
> 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.
>
Yes, this was misunderstanding. I thought you meant defining with the *a,
and not with a,b,c, etc....Thanks anyway :)
>>
>> > 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).
>
No of course not - but it is interesting...
P.
More information about the Python-list
mailing list