mutable default parameter problem [Prothon]
Mark Hahn
mark at prothon.org
Fri Jun 18 04:53:03 EDT 2004
Grégoire Dooms wrote:
> But I wonder what the actual semantics of the second proposal are.
>
> You say the default argument is evaluated in its declaration context
> at each call. But is that evaluation context garanteed to be the same
> at every call ?
>
> #Ex:
> x=0
> def t(a,b,c=x+1):
> # could I assert c==1 if t called with 2 args ?
No, because x could change value. If you wanted that then you would say
c=1. When you say c = x+1 you get x+1, no more, no less. You can code it
to get whatever you want.
> def make_some_obj():
> return []
> def t(a,b,c=make_some_obj()):
> # can I assert c==[] if t called with 2 args ?
This case would always give you [], unless you redefined make_some_obj :-)
This is a dynamic language after all.
> I think every data coming from outside the expression (the variables
> in the expression including the global variables accessed from a
> function) should be preserved. Let aside the eventual mutable state
> stored in an object:
>
> #Ex:
> class Inc:
> def __init__(self):
> self.x=0
> def newval():
> self.x += 1
> return self.x
> x=Inc()
> def t(a,b,c=x.newval()):
> # can I assert c will increase by 1 at each call with 2 args ?
If you really want this then you should be asking for solution 3, which just
keeps the results of the expression evalutation at definition time and then
makes a copy at each call. You would be the only one asking for solution 3
so far. It appears that we are going with solution 2.
> Nice, I had never heard about Prothon. I'll give it a look.
You can find it at http://prothon.org. Hang out on the Prothon mailing list
for a while. We are discussing some really fun stuff.
More information about the Python-list
mailing list