var or inout parm?

Chris Rebert clp at rebertia.com
Fri Dec 12 13:44:31 CET 2008


On Fri, Dec 12, 2008 at 4:34 AM, sturlamolden <sturlamolden at yahoo.no> wrote:
<snip>
> You cannot modify parameters by rebinding. x = x + 1 is a rebinding. x
> += 1 is not.

Python begs to differ, as those two statements are both semantically
identical in this case:

Python 2.6 (r26:66714, Nov 18 2008, 21:48:52)
[GCC 4.0.1 (Apple Inc. build 5484)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 2
>>> id (x)
8405372
>>> x += 1
>>> id(x)
8405360
>>> x = x + 1
>>> id(x)
8405348

If you were talking about lists rather than integers though, you'd be
absolutely correct, as the += ends up being a method call to __iadd__
instead of a plain assignment.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list