newbie question
Alex Martelli
aleax at aleax.it
Sun Jan 13 10:29:25 EST 2002
rihad wrote:
> Hi, I'm new to Python and am making my way through the bundled
> tutorial. As I now understand, variables in Python are really
> references (everything is passed by object reference). If I have this
Yes. Variables are just names, tags, references, labels that
are transiently attached to objects.
> code:
>
> def f(n, x = []):
> # some code
>
> Even if I don't modify x inside f(), is it guaranteed that x will
> always be [], that is, the empty list [] will never be shared with
> some unrelated reference, which could modify it, and thus affect what
> x binds to?
No. The empty list [] is just the DEFAULT value of argument x to function
f. If and when f is called with two arguments, local name x in f is bound
to the second actual argument. The object (a list, originally empty) that
is x's default value stays around even between calls to f, and during a
call to f with two arguments -- but, said object is only accessible (via
local name x in f) during calls to f with a single argument.
That object will never be "shared" with any other name unless you arrange
for that explicitly -- e.g. by having f "return x" and binding the return
value to some other name.
Alex
More information about the Python-list
mailing list