Beginner question - How to effectively pass a large list

Asun Friere afriere at yahoo.co.uk
Thu Dec 18 18:29:55 EST 2003


"J.R." <j.r.gao at motorola.com> wrote in message news:<broje1$abb$1 at newshost.mot.com>...


> 1. There is no value passed to the default argument
> The name "d" is bound to the first element of the f.func_defaults. Since the
> function "f" is an
> object, which will be kept alive as long as there is name (current is "f")
> refered to it, the
> list in the func_defaults shall be accumulated by each invoking.
> 

...

> 
> I think we could eliminate such accumulation effect by changing the function
> as follow:
> >>> def f(d=[]):
>         d = d+[0]
>         print d
> 

And the reason this eliminates the accumulation is that the assignment
('d = d + [0]') rebinds the name 'd' to the new list object ([] +
[0]), ie. it no longer points to the first value in f.func_defaults.

What surprised me was that the facially equivalent:
>>> def f (d=[]) :
...    d += [0]
...    print d
did not do so.  Apparently '+=' in regards to lists acts like
list.apppend, rather than as the assignment operator it looks like.




More information about the Python-list mailing list