[Tutor] slice lists and slicing syntax questions

Michael Langford mlangford.cs03 at gtalumni.org
Sat Oct 13 16:32:39 CEST 2007


On 10/12/07, gregg at lind-beil.net < gregg at lind-beil.net> wrote:
>
> I have been using Python for years now, in all kinds of environments, but
> example:  x is vector of length 5, with value "a","b","c","d","e" , then:
>
> x[3,1,1,1,3,2] # gives [d, b, b, b, d, c]
>
> What is the python equivalent?

  a.  Am I understanding the situation correctly?

When you call [] on an object, it calls __getitem__The definition for
getitem is __getitem__(self,key), where key can be an integer or a slice
object. Slice objects are either a range or a range+step. You've got the
right picture

  b.  Why was this particular way of doing slices chosen?


Dunno. I'll leave  this to the language historians. I  will say a
differently implemented slice object interface would lend itself to novel
types of slicing, like you're talking about. If, for instance, the slice
object would return an iterator of indicies, you could add this case to the
indexable objects. However, the way it currently is, I don't see that being
possible. Also, the way it currently is, I don't see it working well with
all the code that implements __getitem__ as it's currently written even if
slice was implemented.

  c.  What is the best solution for "masking" vectors?
>

I'm with a decorator, you can get to at least x(4,3,3,3,2,1) returning the
type of list you want. At the same time, I'm not sure you're going to be
able to override the __getitem__ prototype without some serious pain.I've
never tried to change the function/method signature with a decorator, but
I'm pretty sure its possible at least for the non-builtin attributes.

You may want to try to write a PEP for python 3000. So much is being changed
with that, you may get it in.

                     --Michael

-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071013/49b37ad2/attachment.htm 


More information about the Tutor mailing list