mutable default parameter problem [Prothon]
Andrea Griffini
agriff at tin.it
Wed Jun 16 17:48:21 EDT 2004
On Wed, 16 Jun 2004 12:40:10 -0700, "Mark Hahn" <mark at prothon.org>
wrote:
>In Prothon:
>
> def foo(x):
> print foo.list.append!(x)
> foo.list = []
>
>(Sorry. I couldn't resist bragging.)
The very first thing I tried was assigning foo.list
outside of the function (and, by the way, that works in
python too); this however doesn't mimic C++ static,
as initialization of the static local variable is done
in C++ when (and only IF) the function is entered.
The value used for initialization can for example
depend on local parameters or global state at *that time*.
Using "hasattr" seemed ugly to me at first, but after
all you need an additional flag anyway, so why not
checking the presence of a certain key in foo.__dict__ ?
That way both initialization and setting the flag are
done at the same time using just one clean statement.
The only (very small) syntax price is the if (that in
C++ is implicit in the overused keyword "static").
Andrea
More information about the Python-list
mailing list