On 09/08/11 08:44, Robert Johansson wrote:

def rp_bounded_sum(n, i = 0, p = (0, 1), q = (1, 1), S = set([(0, 1),
(1, 1)])):

Strangely, (to me) it seems that the function remembers the set S
between different calls:

That's correct. Python evaluates the default parameter value once at function definition time so every call to the function uses the same default object value. This often catches out newbies but should be considered a 'feature of the language' rather than a bug - it's existence enables some useful techniques which would not otherwise be possible :-)

If you check the documentation on default parameters you will find more details.

hth,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to