473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is it possible to merge xrange and slice?

Bas
Hi,

stupid question, but would it be possible to somehow merge xrange
(which is supposed to replace range in py3k) and slice? Both have very
similar start, stop and step arguments and both are lightweight
objects to indicate a range. But you can't do a[xrange(10,20)] and
'for i in slice(10,20)'. The only difference is see is some behavior
with infinities (e.g. object[3:] gives a slice(3,maxint) inside
_getitem_ , but I think this should not be a large problem
(xrange(0,infinity) could just yield a generator that never stops).

Which problems am I overlooking that prevent this?

Cheers,
Bas

Apr 30 '07 #1
3 2966
Which problems am I overlooking that prevent this?

1. Generators and slices serve two distinctly different tasks.
2. They may have the similar interfaces, but are implemented
differently. Each optimized for its specific task.

You are essentially asking "Why not do it?", to which I respond "Why
do it?". How does this consolidation of features improve Python?

Apr 30 '07 #2
On 30 Apr 2007 11:02:19 -0700, Bas <we*****@gmail.comwrote:
stupid question, but would it be possible to somehow merge xrange
(which is supposed to replace range in py3k) and slice? Both have very
similar start, stop and step arguments and both are lightweight
objects to indicate a range. But you can't do a[xrange(10,20)] and
'for i in slice(10,20)'. The only difference is see is some behavior
with infinities (e.g. object[3:] gives a slice(3,maxint) inside
_getitem_ , but I think this should not be a large problem
(xrange(0,infinity) could just yield a generator that never stops).

Which problems am I overlooking that prevent this?
Novel idea but how would slice(3,-1) work?

--
mvh Björn
Apr 30 '07 #3
On Mon, 30 Apr 2007 21:00:15 +0000, BJörn Lindqvist wrote:
On 30 Apr 2007 11:02:19 -0700, Bas <we*****@gmail.comwrote:
>stupid question, but would it be possible to somehow merge xrange
(which is supposed to replace range in py3k) and slice? Both have very
similar start, stop and step arguments and both are lightweight
objects to indicate a range. But you can't do a[xrange(10,20)] and
'for i in slice(10,20)'. The only difference is see is some behavior
with infinities (e.g. object[3:] gives a slice(3,maxint) inside
_getitem_ , but I think this should not be a large problem
(xrange(0,infinity) could just yield a generator that never stops).

Which problems am I overlooking that prevent this?

Novel idea but how would slice(3,-1) work?
Since the semantics of slice and xrange are different, you get very
different results:

>>slice(3, -1)
slice(3, -1, None)
>>range(10)[3:-1]
[3, 4, 5, 6, 7, 8]
>>xrange(3, -1)
xrange(0)

I don't see why you would want to mix'n'match slice objects and xrange
objects like Bas wants. I *think* what he's imagining is that if you say
something like:

alist[5:10] # a slice from a list

it should be the same as

alist[xrange(5, 10)]
That would, I think, only make sense if Python lists allowed sequence
arguments for slicing, as well as ranges:

alist[5] # gives the fifth element
alist[5:11] # gives the fifth through tenth elements
alist[5,7,10] # gives the 5th, 7th and 10th elements

Then alist[xrange(5, 10)] would be the same as alist[5, 6, 7, 8, 9].

I can see that could be useful, especially if (like slicing) it was
tolerant of errors. We could imagine list's __*slice__ methods accepting
either a single integer index, a slice object, or a tuple object.

The actual mechanics of what something like this would mean:

del alist[2, 4, 7, 1, 1]

I leave open.

While this could be interesting, I'm not sure that it belongs in slicing.
Although if not, that would mean growing three methods, a getter, a
setter and a deleter. Hmmm. Maybe slicing should be extended to accept
a sequence of indices...

.... enough dreaming... even if list slicing accepted a sequence argument,
passing an xrange object would not be the way to do it. That would be like
replacing:

result = alist[5:10]

with:

result = []
for i in (5, 6, 7, 8, 9):
result.append(alist[i])

--
Steven D'Aprano

May 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christopher T King | last post by:
Okay, so this is really two requests in one, and they're both kinda outlandish, but I'm gonna post them nonetheless: I've always thought xrange() to be ugly; it looks to be a lot of typing just...
7
by: Christian Neumann | last post by:
Hello, i have a problem with the built-in function xrange(). Could you by any chance be able to help? I use Python 2.3.4 (final) and i think there is a bug in the built-in
1
by: Nicolas Lehuen | last post by:
Hi, I've posted this patch on Source forge : http://sourceforge.net/tracker/index.php?func=detail&aid=1391204&group_id=5470&atid=305470 If you want to update a dictionary with another one,...
29
by: Steve R. Hastings | last post by:
When you compile the expression for i in range(1000): pass does Python make an iterator for range(), and then generate the values on the fly? Or does Python actually allocate the list and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.