Default Parameters to function!

Dennis E. Hamilton infonuovo at
Thu Mar 16 05:15:09 CET 2000


The behavior in your example is highly desirable.  It is the Python
idiom for creating a persistent global entity that is lexically private
to the function you are creating.  (You could implement a random number
generator using a similar device.)

Since this is the *only* idiom for this in Python, I would not want the
behavior changed.  Also, the fact that Python evaluates (binds) def's at
first-import time is very useful and practical.  Knowing that defaults
are bound to predictable default values is far more important to me than
having them be symbolically link in the scope of execution, with
potential weird debugging problems.

Clearly, there is no "right" answer here.  I am one of the people who
prefer the current approach until there is something less idiosyncratic
in the language that provides comparable behavior.

Meanwhile, if you intend to have the evaluation of a function depend on
the current state of global variables, a practice I prefer is to provide
such information as explicit parameters, so the dependency is explicitly
recognized and accounted for in both the definition and at all uses of
the function.  I am not sure what you would like the Test function to be
able to do different from what Python has it do, but I bet there is a
straightforward way to do that already.

I don't want to argue you out of your request.  I think it valuable to
know what others find valuable in Python and in handling the situation
you describe.  I am sure there are many other views here, none
authoritative except for GvR!

-- Dennis

Dennis E. Hamilton
mailto:infonuovo at
tel. +1 (206) 779-9430 (gsm)
fax. +1 (425) 793-0283

-----Original Message-----
From: python-list-admin at
[mailto:python-list-admin at]On Behalf Of Yuriy Gorvitovskiy
Sent: Wednesday, 15 March 2000 12:14
To: python-list at
Subject: Default Parameters to function!

This is the example:

def Test(Value,iter=5,Map={}):
    if iter>0:
    return Map

def TestCall():
    print Map
    print Map


This is the result:

{4: 5, 3: 5, 2: 5, 1: 5, 0: 5}
{9: 6, 8: 6, 7: 6, 6: 6, 5: 6, 4: 5, 3: 5, 2: 5, 1: 5, 0: 5}

More information about the Python-list mailing list