Newbie list question
Matthew Alton
Matthew.Alton at Anheuser-Busch.COM
Fri Jul 13 12:43:08 EDT 2001
I am a UNIX/C programmer w/15y experience. Forgive me if my neural
pathways are all about following pointers. The adjustment to
Python-think is bumpy but I'll get by with a little help from my
friends, eh?
Here's the crux of the biscuit:
>>> foo = ['a', 'b', 'c'] # We have a list named 'foo.' Excellent.
>>> bar = foo # bar points to foo. Or does it?
>>> baz = foo[:] # baz is a copy of foo.
>>> foo
['a', 'b', 'c']
>>> bar
['a', 'b', 'c']
>>> baz
['a', 'b', 'c'] # So far, so good.
>>> del foo[2] # Get rid of 'c' in foo and, therefore in
bar (?)
>>> foo
['a', 'b'] # 'c' is gone from foo...
>>> bar
['a', 'b'] # ... and also from bar, as expected.
>>> baz
['a', 'b', 'c'] # baz, the copy, is unaffected. Also as
expected.
>>> foo = foo + ['c'] # Add 'c' back to foo.
>>> foo
['a', 'b', 'c'] # 'c' is back. Good.
>>> bar
['a', 'b'] # ??? What the... ??? Where is 'c'?
>>> baz
['a', 'b', 'c'] # baz still unaffected, of course.
>>>
I have verified this behavior on Python 1.5.1 (AIX 4.3.3) and on
Python 2.1
(Solaris Sparc 2.8). From my arcane perspective, this behavior is
utterly inconsistent and confusing. I strongly suspect that I simply
do not correctly grok the list structure, but so far I am unable to
turn up an explanation in the literature.
Any help is appreciated.
More information about the Python-list
mailing list