Something in the function tutorial confused me.

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Aug 6 14:08:33 EDT 2007


On Mon, 06 Aug 2007 10:51:20 -0700, Lee Fleming wrote:

> why isn't the y in def f (x, y = []): something
> garbage-collected?

`y` is a name.  Only objects are garbage collected.  There is no `y` in
that ``def`` in the sense that a local name `y` exists when the ``def`` is
executed.  The line just says there will be a local name `y` if
the function `f()` is executed and that local name will be bound to the
given object.  Which happen to be a list.  This list is referenced by the
function object, so it won't get garbage collected, and it is bound to a
local name `y` every time the function is called.  It is always the
very same list object.  And if you mutate it, this will be visible to
other calls to the function.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list