[Python-Dev] RE: iterable slices

Damien Morton Damien.Morton@acm.org
Thu, 2 May 2002 03:49:20 -0400


> >>> slice(3)
> slice(None, 3, None)
> >>>

This is where it breaks down.

You'd have to do some buggering around with the start/stop/step
attributes to make it work. There wouldn't be a direct correspondance
between the slice/tuple element positions and the start and stop
attributes.

a = slice(3) 
a[0]==3, a.start==None, a.stop==3, a.step==None

b = slice(1,3)
b[0]==1, b[1]==3, b.start=1, b.stop=3, b.step=None


By supplying your own factory I was thinking of something like the
range() function.

for i in weirditer(a:b:c:d:e):

Probably not very usefull, given that you can already do that without
having to resort to slices.

for i in weirditer(a,b,c,d,e):


Since lists happily deal with empty slices [1,2,3,4,5][:], I don't see
any reason why youd forbid them at higher (or lower) dimensions. The
issue with something like:

(::)

Is the line-noise quality of it. Its not too bad though.

You can push this idea into the realms of the extremely strange by
imagining nested slices and slices of tuples and such.

(1:(2:3:(1,2,3)):("hello",f(y)))



> -----Original Message-----
> From: Alex Martelli [mailto:aleax@aleax.it] 
> Sent: Thursday, 2 May 2002 03:21
> To: Damien Morton; python-dev@python.org
> Subject: Re: [Python-Dev] RE: iterable slices
> 
> 
> On Thursday 02 May 2002 09:05, Damien Morton wrote:
> 	...
> > I hadnt thought of that, but its kind of elegant.
> >
> > for i in (a:b:c:d:e):
> >
> > Would probably be an error. Trying to make an iterator out 
> of anything 
> > but a 2 or 3 element slice should fail, unless you supply your own 
> > iterator factory.
> 
> Currently you can't "supply your own factory" between iter() 
> and a given object type -- iter() looks for the object's 
> __iter__ (&c), not in any 
> registry of adapter-factories.  PEP 246 might be tweaked to 
> change that, but it would be a doubtful change IMHO.
> 
> 
> > Omitted slice elements being None is a nice idea, and would 
> work with 
> > the RDF notation
> >
> > (subj::obj) -> slice(subj, None, obj)
> >
> > It gets a little hairy when you start doing things like 
> this, however
> >
> > (::) -> slice(None, None, None)
> 
> You could forbid it:
> 
> >>> slice()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: slice() takes at least 1 argument (0 given)
> 
> > And what then of the 1-element slice? (if its necessary at all)
> 
> It definitely is (to indicate "all items from i" or "all 
> items up to i", depending on which 'element' you mean, start or stop).
> 
> > (1:) -> slice(1, None) or slice(1)??
> 
> >>> slice(3)
> slice(None, 3, None)
> >>>
> 
> i.e., like (:3).  (1:) must clearly mean "all from 1 upwards" 
> by analogy with sequence slicing such as somelist[1:].
> 
> 
> Alex
>