That might be the case for more complex objects...
jstroud at mbi.ucla.edu
Sat Apr 14 22:17:24 CEST 2007
Bart Willems wrote:
> Dennis Lee Bieber wrote:
>> On 14 Apr 2007 06:35:34 -0700, "jamadagni" <samjnaa at gmail.com> declaimed
>> the following in comp.lang.python:
>> In Python, the "variable" NAME does NOT define storage; unlike most
>> other classical languages where the "variable name" is a storage
>> address, and the value of the RHS is COPIED to that address. Python does
>> not do such copying. Names are references to the RHS object itself.
>> a = 5
>> means that somewhere in memory is an integer object with the value "5";
>> the name "a" is now "pasted onto" that integer object.
>> b = a
>> finds the object that has the name "a" stuck to it, and sticks a second
>> name "b" onto the same object. There is still only one "5" in memory.
> I can try this in interactive mode:
> >>> a = 5
> >>> b = a
> >>> a += 1
> >>> print b
> So, if /a/ and /b/ where pointing to the *same* "5" in memory, then I
> would expect b to be increased, just as a. But after increasing a, b is
> still 5...
> Lists behave as described above, integers and floats don't.
> By the way, a classic language like C has features like this too;
> they're called pointers.
I think that after a += 1, a memory location with a 6 is created and now
a points to that because += has assignment buried in it.
More information about the Python-list