Default Parameters to function!

Timothy Docker timd at macquarie.com.au
Wed Mar 15 21:35:20 EST 2000


Tim Peters <tim_one at email.msn.com> writes:

> > suggesting that that the current behaviour is not the ideal one.
> 
> It certainly is not.  OTOH, do we have evidence that the other behavior is?
> The notion that there is *an* "ideal behavior" here seems exceedingly
> dubious to me.  If after:
> 
> import random
> def shuffle(seq, random=random.random):
>     """Permute seq randomly in place.
> 
>     Optional arg random is a no-argument function returning a
>     random real in [0, 1).  By default, random.random is used.
>     You may wish to supply a more powerful generator for high
>     resolution experiments.
>     """
> 
>     # etc, w/ code invoking random()
> 
> del random  # so "import *" doesn't pollute the caller's namespace

Oh boy, for any suggestion, there's always an endless source of taxing
counter-examples!

My (admittedly slight) interest in this is that I have had to explain
the behaviour to new users a few times, and there's a mild look of
disapointment when python isn't behaving as expected for the first
time. (That's assuming they haven't tried dividing integers yet).

I had imagined that, assuming lexical scoping in py3k, the parameters
could be implied lexically scoped lambdas, taking no arguments. Of
course, having said this, I'm not certain this would work for the
above case!!

As you say, the current behaviour does have the benefits of easy
explanation and implementation.

Tim



More information about the Python-list mailing list