a wierd parameter passing behavior

DavidW Blaschke dwblas at yahoo.com
Wed Jun 4 21:59:44 CEST 2003


It's not good practice to use the letters "l", "o",
or "i" as variable names as they look too much like
numbers and hence the code is hard to read.  Just some
friendly advice.

D.W.

> --- In python-list at yahoogroups.com, mpradella at y...
> (Torvo Sollazzo) 
> wrote:
> > Hi!
> > 
> > I found a strange parameter passing behavior in
> Python (v. 2.2.2).
> > Look at this piece of code:
> > def p(l) :
> >     l += [3]
> >     l[0] = 2
> > 
> > def p1(l) :
> >     l = l + [3]
> >     l[0] = 2
> > 
> > def f() :
> >     l = [1]
> >     p(l)
> >     print l
> >     l = [1]
> >     p1(l)
> >     print l
> > 
> > If I call f(), I obtain the following results:
> > [2,3]
> > [1]
> > Why p1() is different from p()? The list l should
> be passed by
> > reference in both cases, while it seems that "l =
> l + [3]" creates a
> > new local variable, also called l.
> > 
> > Any idea?
> > Many thanks,
> > TS
> > -- 
> >
> http://mail.python.org/mailman/listinfo/python-list
> 


__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com





More information about the Python-list mailing list