[Tutor] function remember calls?

Alan Gauld alan.gauld at btinternet.com
Tue Aug 9 10:05:18 CEST 2011

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 


Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list