Something in the function tutorial confused me.
whamil1 at entergy.com
Mon Aug 6 19:30:04 CEST 2007
> From: Lee Fleming
> On Aug 6, 6:25 am, Neil Cerutti <horp... at yahoo.com> 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.
When you call f(23), the variable y within it gets created and points at
None. When f(23) exits, the y that it created gets destroyed. (Well,
goes out of scope, but even if it's not garbage collected it won't ever
come back into scope.) When you then call f(24), a new y is created
that also points to None, and disappears forever when f(24) exits.
The values in a def statement are created when the def is executed, but
the variables are only created when the function is actually called, and
new ones are created every time the function is called.
More information about the Python-list