Bulba! wrote: > [...] > But why the following happens? > > >>>>def j(a,i=0): > > ... i=i+1 > ... return (a, i) > ... > >>>>j(2) > > (2, 1) > >>>>j(3) > > (3, 1) > >>>>j(5) > > (5, 1) > > > From Language Reference: > > http://www.python.org/doc/2.3.4/ref/function.html > > "Default parameter values are evaluated when the function definition > is executed. This means that the expression is evaluated once, when > the function is defined, and that that same ``pre-computed'' value is > used for each call. This is especially important to understand when a > default parameter is a mutable object, such as a list or a dictionary: > if the function modifies the object (e.g. by appending an item to a > list), the default value is in effect modified. " > > > Surely the variable i is a mutable object? > [...]
No, i is not mutable! i is a name for a number object which is _not_ mutable (http://www.python.org/doc/2.4/ref/types.html). The line i=i+1 creates a new number object with the value 1 and gives it the name i. Bye, Dennis -- http://mail.python.org/mailman/listinfo/python-list