function call - default value & collecting arguments

Primoz Skale primoz.skale.lists at gmail.com
Wed Apr 2 16:32:36 EDT 2008


>> 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?

>
> 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! :)

Primoz





More information about the Python-list mailing list