[ python-Feature Requests-1191697 ] slice indices different than integers

SourceForge.net noreply at sourceforge.net
Fri Apr 29 04:31:00 CEST 2005


Feature Requests item #1191697, was opened at 2005-04-28 08:42
Message generated for change (Comment added) made by cxdunn
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1191697&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Sebastien de Menten (sdementen)
Assigned to: Nobody/Anonymous (nobody)
Summary: slice indices different than integers

Initial Comment:
Hi,

Slice notation is quite convenient while addressing some 
item in a collection.
It would be nice to extend this notation by enabling any 
object instead of integer in slices.

Example 1: let time_serie be an object representing a 
time serie and date and days object that manages dates 
and concept of days interval, one could indexed 
time_serie with:

time_serie[date(2004,3,4):date(2004,5,3)]
or
time_serie[date(2004,3,4):date(2004,5,3):days(5)]

Example 2:
Let f be a numerical function with multiple arguments, 
one could get an array of results by using

f[3:10:2, 1:5:3]
or naturally :-)
f[3.2:10.1:0.4, 1:5:3]

Well, I think it is a matter of removing the check on 
argument of slices to enable the syntactic sugar 
start:end:step in __getitem__ calls as well as adding 
integer checks on slice attributes when using it in old 
way.

----------------------------------------------------------------------

Comment By: Christopher Dunn (cxdunn)
Date: 2005-04-28 21:31

Message:
Logged In: YES 
user_id=1267419

This already works. See page 95 of Python in a Nutshell, or
check the docs. 

Slice objects are created when you specify a slice. They can
contain anything. Special methods (like __getitem__ and
__delitem__) are passed that object, so your own class could
define those methonds and do anything you want with the
arbitrary slice object. For example:
<pre>

class Ruler(object):
    def __getitem__(self, s):
        return s.stop - s.start

x = Ruler()
print x[5:7]
print x[4.2:5.1]
</pre>
produces
2
0.9

-cxdunn

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1191697&group_id=5470


More information about the Python-bugs-list mailing list