Default Parameters to function!

Paul Prescod paul at
Fri Mar 17 07:07:15 CET 2000

Tim Peters wrote:
> 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

I think you need a better example than that. Only smart-alec brown belts
go around treating modules and functions like variables! Don't we
usually cater to the beginning user, not the brown belt? It is easy to
work around by renaming the module.

The fact that Python late-binds function names can be annoying and
dangerous outside of default arguments. Just last week I did this:

import random
def shuffle(seq):
     random.random( )
del random  # so "import *" doesn't pollute the caller's namespace

Shows how much I know about Python! I only think about functions "like
that" when it is convenient. :)

There may be an argument from reliability (and certainly from
performance) in resolving certain names statically as part of the
language specification. The REPL might have to be a special case...

 Paul Prescod  - ISOGEN Consulting Engineer speaking for himself
If all you want is sleep, go to bed.
But if you want to dream, go to Barbados.
    - Timothy Findley, "Barbados: The Very Pineapple of Perfection"

More information about the Python-list mailing list