The “does Python have variables?” debate

Johannes Schneider johannes.schneider at galileo-press.de
Thu May 8 03:26:57 EDT 2014


On 08.05.2014 02:35, Ben Finney wrote:
> Marko Rauhamaa <marko at pacujo.net> writes:
[..]
> Python, on the other hand, has this behaviour::
>
>      foo = [1, 2, 3]
>      bar = foo          # ‘bar’ binds to the value ‘[1, 2, 3]’
>      assert foo == bar  # succeeds
>      foo[1] = "spam"    # ‘foo’ *and* ‘bar’ now == [1, "spam", 3]
[..]

IMHO this is the behavior of having a variable pointing to it's value; 
foo to the list and bar to foo.


consider the following:
 >>> def f(l):
...     l[1] = 'foo'
...
 >>> l1 =  [1,2,3]
 >>> f(l1)
 >>> l1
[1, 'foo', 3]

this means, l1 consists of "pointers" to its values.
Otherwise, it's not calling by reference, because

 >>> g(l1)
 >>> l1
[1, 'foo', 3]

does not change l1. Once again, if I pass an object it behaves like 
calling by reference:

 >>> class A:
...     a = 0
...
 >>> a = A()
 >>> a.a
0
 >>> def h(a1):
...     a1.a = 1
...
 >>> h(a)
 >>> a.a
1

bg,
Johannes


-- 
Johannes Schneider
Webentwicklung
johannes.schneider at galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn



More information about the Python-list mailing list