list.remove for Novices

Arthur Siegel ajs at ix.netcom.com
Sun Nov 25 17:12:03 EST 2001


Tim writes -

>I expect every newbie gets bit by this at least once.  This is the first
>time I've seen it called "apparent", though (nobody actually said that,
>right?).  It's a subtlety, as the Reference Manual points out in a bold
>Warning. 

But the Reference Manual is for the Legal profession. I'd suggest a
 tutorial warning. 

I would say, in fact, that whether I am working on the thing itself, or a
copy of the thing itself, and leaving the thing unchanged, is the #1
issue of confusion for the novice - if I am typical (and I have always
posited that I am).  Especially when there are few thing1=thing2 
thingies floating around.   

>>>b=[1,2,3,4]
>>>a=b.reverse()
>>>b=a
>>>print b
None

???

I get it, but it takes a while - and errors were rampant, and this form
of confusion comes in many flavors. I have had occasion to savor 
many of them already.

The 

for p in x:
   x.remove(p) 

surprise just being a new dessert topping.

Is anyone aware of a reference where these issues are given
focus and covered clearly and comprehensively 
for the Python novice??

Not that someone like me is likely to read it carefully.  Just
so much damage one can do screwing up a bezier curve, and
I tend to live dangerously - and learn by messing up enough
times until something eventually clicks.

> If you mutate a container while traversing its contents, every
>language supporting such a thing does something that will surprise someone
>(can't help it -- some people will be surprised if their mutations aren't
>visible during the traversal, while others will be surprised if they are,
>and the rest will complain if you raise an exception).

Backwards compatibility issues aside  (are there actually folks who
take advantage of the current behavior?) isn't it pretty clear that the
current behavior is probably the most surprising of the possibilities - 
at least to the naive eye.  

My first PEP?

Art





More information about the Python-list mailing list