just curious

Michael Peuser mpeuser at web.de
Sat Sep 6 09:17:51 CEST 2003


"Elaine Jackson" <elainejackson7355 at home.com> schrieb im Newsbeitrag
news:rnd6b.904924$ro6.18233500 at news2.calgary.shaw.ca...
> I'm new to Python, and I've noticed the following:


(1) Python has a quite strict "call by value" similar to C. This means - as
in C) you can use formal parameters as if they were local variables. (And
there are some useful tricks with that..)
So A = A + B will have no outside impact.

(2) A += B for lists is a shortcut for
     A.extend(B)
which means that it changes something A is bound to (or "points to" as you
would say in C). This is somewhat awkward because it sometimes works counter
intuitive as in your case. Just keep in mind: A+=B is *not*  A=A+B but
behaves as if in most cases.... ;-)

Kindly
Michael P





>
> >>> def f(a,b):
>  a+=b
> >>> def g(a,b):
>  a=a+b
> >>> p=[1,2,3]
> >>> q=[4,5,6]
> >>> r=[7,8,9]
> >>> s=[10,11,12]
> >>> f(p,q)
> >>> p
> [1, 2, 3, 4, 5, 6]
> >>> g(r,s)
> >>> r
> [7, 8, 9]
>
> Any deep reason for this, or "just because"? TIA.
>
> Peace,
> EJ
>
>






More information about the Python-list mailing list