Beginner question - How to effectively pass a large list

Jp Calderone exarkun at intarweb.us
Thu Dec 18 20:04:39 EST 2003


On Thu, Dec 18, 2003 at 03:29:55PM -0800, Asun Friere wrote:
> "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.

  list.extend, to be more precise, or list.__iadd__ to be completely precise
:)  The maybe-mutate-maybe-rebind semantics of += lead me to avoid its use
in most circumstances.

  Jp

> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 





More information about the Python-list mailing list