RE: Something in the function tutorial confused me.
- From: "Hamilton, William " <whamil1@xxxxxxxxxxx>
- Date: Mon, 6 Aug 2007 12:30:04 -0500
From: Lee Fleming
On Aug 6, 6:25 am, Neil Cerutti <horp...@xxxxxxxxx> 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.
--
-Bill Hamilton
.
- Follow-Ups:
- Re: Something in the function tutorial confused me.
- From: Lee Fleming
- Re: Something in the function tutorial confused me.
- References:
- Re: Something in the function tutorial confused me.
- From: Lee Fleming
- Re: Something in the function tutorial confused me.
- Prev by Date: Re: Tkinter or wxpython?
- Next by Date: Re: Tkinter or wxpython?
- Previous by thread: Re: Something in the function tutorial confused me.
- Next by thread: Re: Something in the function tutorial confused me.
- Index(es):
Relevant Pages
|