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

SourceForge.net noreply at sourceforge.net
Fri Apr 29 11:53:05 CEST 2005

Feature Requests item #1191697, was opened at 2005-04-28 14:42
Message generated for change (Comment added) made by mwh
You can respond by visiting: 

Category: Python Interpreter Core
Group: None
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Sebastien de Menten (sdementen)
>Assigned to: Michael Hudson (mwh)
Summary: slice indices different than integers

Initial Comment:

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:


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 


>Comment By: Michael Hudson (mwh)
Date: 2005-04-29 10:53

Logged In: YES 

Closing, because you can do this already:

>>> class D(object):
...  def __getitem__(self, item):
...   print item.start, item.stop, item.step
>>> import datetime
>>> D()[datetime.datetime.now():1.3:D]
2005-04-29 10:56:17.368061 1.3 <class '__main__.D'>


Comment By: Christopher Dunn (cxdunn)
Date: 2005-04-29 03:31

Logged In: YES 

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:

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

x = Ruler()
print x[5:7]
print x[4.2:5.1]



You can respond by visiting: 

More information about the Python-bugs-list mailing list