a wierd parameter passing behavior

Gerrit Holl gerrit at nl.linux.org
Wed Jun 4 09:44:41 EDT 2003


Torvo Sollazzo schreef op woensdag  4 juni om 15:31:51 +0000:
> def p(l) :
>     l += [3]
>     l[0] = 2
> 
> def p1(l) :
>     l = l + [3]
>     l[0] = 2

> Why p1() is different from p()?

p1 does not change l, p does. p1 creates a new l in it's own local
namespace, while p updates l: += equals .append(). This is documented
in the language reference:

"""
An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead.
"""
http://www.python.org/dev/doc/devel/ref/augassign.html

yours,
Gerrit.

-- 
206. If during a quarrel one man strike another and wound him, then he
shall swear, "I did not injure him wittingly," and pay the physicians.
        -- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list