473,668 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,infin ity) could just yield a generator that never stops).

Which problems am I overlooking that prevent this?

Cheers,
Bas

Apr 30 '07 #1
3 2982
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,infin ity) 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,infi nity) 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(a list[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
1412
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 to get an efficient loop over some numbers. Since it uses the same 'parameters' as the slice operator, I was thinking it would be neat if the slice operator were to be syntactic sugar for the xrange operator. Example: for i in xrange(0,8,2):
7
2577
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
1330
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, you can simply use update : a = dict(a=1,c=3)
29
2864
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 then step through it? I was under the impression that recent releases of Python optimize this
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.