Beginner question - How to effectively pass a large list
exarkun at intarweb.us
Fri Dec 19 02:04:39 CET 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+
> > print d
> And the reason this eliminates the accumulation is that the assignment
> ('d = d + ') rebinds the name 'd' to the new list object ( +
> ), 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 += 
> ... 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.
More information about the Python-list