newbie question

maxm maxm at mxm.dk
Sun Jan 13 16:20:49 EST 2002


"rihad" <rihad at mail.ru>

> 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?

Maybe what you want is::

def f(n, x = None):
   if x == None: x = []
   # some code


Then x will not be shared with anything else. So don't use a mutable object
as default value in a parameterlist in that case.

regards Max M





More information about the Python-list mailing list