list.remove for Novices

Steve Holden sholden at holdenweb.com
Mon Nov 26 14:13:46 CET 2001


"Arthur Siegel" <ajs at ix.netcom.com> wrote ...
> 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.
>
Along the lines of "Sitting on the branch you are sawing off the tree is not
a good idea"?

> 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.
>
Art, surely you've been using Python long enough to realise that assignment
never creates copies?

> >>>b=[1,2,3,4]
> >>>a=b.reverse()
> >>>b=a
> >>>print b
> None
>
> ???
>
This behavior is very clearly documented for both reverse() and sort() , and
is also in the FAQ.

> 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.
>
Isn't Python FUN?

> The
>
> for p in x:
>    x.remove(p)
>
> surprise just being a new dessert topping.
>
Surprise? There you go, sawing away at that branch again. I realise I won't
convince you, because you've been bitten, that this isn't a situation where
further documentation is necessary. Seems obvious to me, but clearly I'm not
typical of *all* users (thank heavens).

> 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.
>
So you ADMIT that warnings wouldn't do you any good. I suppose they'd be
useful for "other people", though?

> > 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.
>
No. But the current behavior isn't something one would "take advantage of".
It's something one needs an intelligent awareness of to avoid being bitten
in the ass. In other words, one tries to avoid its taking advantage of you!

> My first PEP?
>
I wouldn't even think about it, but of course the point of the PEPs is to
encourage discussion.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list