Mutable default values for function parameters

sebastien s.keim at laposte.net
Fri Oct 5 05:43:01 EDT 2001


> The question was how to reinitialize 'd' (or whatever) when one wants to start
> over after several calls using saved values.
> 
> Terry J. Reedy

You're right. 
The most part of the responses in this trade solve the general problem
caused by mutable default values, but not the one that I asked for.

I imagine that I must firstly blame myself for this situation, because
I wasn't really clear in my question and many have probably confused
it with the classic python problem of mutable default values.

In fact my question is maybe not related to mutable default arguments
at all. This is only the way I used to try to solve my problem wich is
to find a way to mimic this:

def variant(data, old_data):
	r = (data <> old_data[0])
	old_data[0] = data
	return r

A=[None]
for i in range(10):
	assert variant(i,A)

But without mandate the user to explicitely manage the plumbery used
to save the data between two calls of the function (A here).

By the way, I would thanks you for the solutions your suggested in a
previous post. All of theim work, but all off theim need an extra
initialisation work.
This initialisation work isn't functionnal, but is only mandated
because the variant function can't manage itself a kind of data
persistance and need help from the caller.

If I was able to achieve this, variant could maybe become usefull ;-)



More information about the Python-list mailing list