Default Parameters to function!
Yuriy Gorvitovskiy
yuriy at centricsoftware.com
Thu Mar 16 15:55:19 EST 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:
GlobalList=[]
GlobalList.append('Begin')
DefIter = 5
def Test3(Value,iter=DefIter,List=GlobalList):
if iter>0:
List.append(Value)
Test3(Value,iter-1,List)
return List
def TestCall3():
List=Test3(5)
print List
global DefIter
DefIter = 8 """but it will have no effect for the default value in Test3
function (see Output)"""
List=Test3(20)
print List
GlobalList.append('Before Call')
TestCall3()
GlobalList.append('End')
print GlobalList
print DefIter
Output:
['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']
8
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.
[YG]
Dennis E. Hamilton wrote in message ...
>Yuri,
>
>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
>InfoNuovo
>-------------------------
>mailto:infonuovo at email.com
>tel. +1 (206) 779-9430 (gsm)
>fax. +1 (425) 793-0283
>http://www.infonuovo.com
>
>-----Original Message-----
>From: python-list-admin at python.org
>[mailto:python-list-admin at python.org]On Behalf Of Yuriy Gorvitovskiy
>Sent: Wednesday, 15 March 2000 12:14
>To: python-list at python.org
>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
>
>
>TestCall()
>
>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