Lee Fleming wrote: > On Aug 6, 6:25 am, Neil Cerutti <[EMAIL PROTECTED]> wrote: > Because when the function is called, the line > > >> if y is None: y = [] > > > is executed, binding a brand new empty list to y. This > "rebinding" happens every time the function is called, unless you > provide an argument for y that is not None. > > Thanks for the prompt replies. But I am still confused. This is what > confuses me.... > The first time you call the function, say with f(23), after the > function ends, > y no longer equals None. Therefore, calling f again, this time like > this f(24), > should make y equal [23,24], because the 'if y == None' test fails, or > at least I think it > fails, because y.append(x) added something that was not equal to None > during the previous call. > > Please help me! > The thing that doesn't seem to have been stated explicitly is that each function call creates a brand-new namespace. This namespace is initialized from the function definition, which includes assignment of default values to absent keyword arguments. The function definition, then, contains a reference to each default argument value.
In the case that tripped you up, however, the default argument you provided was a mutable object (a list) that can be changed in place by operations like .append(), whereas when you provide a None default any further assignment to the argument name binds it to a new value. In the first case, changes to the object are naturally reflected in later calls, since it is the object referenced in the function definition that is being mutated. It's only really the same difference as a = [1, 2] b = a a.append("three") which modifies a single list to which both names, a and b, are bound, and a = 5 b = a a += 3 which rebinds the name a to a new object, meaning that a and b are no longer bound to the same object. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden --------------- Asciimercial ------------------ Get on the web: Blog, lens and tag the Internet Many services currently offer free registration ----------- Thank You for Reading ------------- -- http://mail.python.org/mailman/listinfo/python-list