Default Parameters to function!

Yuriy Gorvitovskiy yuriy at
Thu Mar 16 21:55:19 CET 2000

Thank You all for this discussion. You convinced me of the usefulness of
this behavior, and I will used it in future. It's OK with me, once I
understand the behavior. As a matter of fact I'm using it now. I'm using it
this way:

DefIter = 5

def Test3(Value,iter=DefIter,List=GlobalList):
    if iter>0:
    return List

def TestCall3():
    print List

    global DefIter
    DefIter = 8 """but it will have no effect for the default value in Test3
function (see Output)"""

    print List

GlobalList.append('Before Call')
print GlobalList
print DefIter

['Begin', 'Before Call', 5, 5, 5, 5, 5]
['Begin', 'Before Call', 5, 5, 5, 5, 5, 20, 20, 20, 20, 20]
['Begin', 'Before Call', 5, 5, 5, 5, 5, 20, 20, 20, 20, 20, 'End']

As I understand the mutable default value can be shared between the Default
value and global variable. For example, if You want to use the module global
List/Map/Class by default, or You can specify You own for the same function.
The only problem with this approach  I have if I use for the default
parameter not mutable type. They can't shared between global variable and
default value. But anyway, I start liking this behavior too.


Dennis E. Hamilton wrote in message ...
>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:
>        Map[len(Map)]=Value
>        Test(Value,iter-1,Map)
>    return Map
>def TestCall():
>    Map=Test(5)
>    print Map
>    Map=Test(6)
>    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