Surprised by the command "del"

castironpi at gmail.com castironpi at gmail.com
Sat Mar 1 20:59:43 CET 2008


On Mar 1, 2:05 pm, "K Viltersten" <t... at viltersten.com> wrote:
> I'm reading the docs and at 5.2 the del
> statement is discussed. At first, i thought
> i've found a typo but as i tried that
> myself, it turns it actually does work so.
>
>   a = ["alpha", "beta", "gamma"]
>   del a[2:2]
>   a
>
> Now, i expected the result to be that the
> "beta" element has been removed. Obviously,
> Python thinks otherwise. Why?!
>
> Elaboration:
> I wonder why such an unintuitive effect has
> been implemented. I'm sure it's for a very
> good reason not clear to me due to my
> ignorance. Alternatively - my expectations
> are not so intuitive as i think.   :)

It's the Phillips vs. the flathead.  Is b in x[a:b:c] an endpoint or a
distance?

This is Python too; you're welcome to implement:
 - b means length
 - one-based indices
 - inclusive
 - b out of bounds exceptions

A slice object (a,b,c) is passed to
whatevercollection.__getitem__, .__setitem__, or .__delitem__.



More information about the Python-list mailing list