[issue46312] range() function could accept slice() objects as parameters

2022-01-09 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

Accepting a slice directly in the range constructor is ambiguous. What to do if 
start or stop are negative or None? What if stop is less than start? You need 
to specify a sequence length to handle these cases.

Maybe the following expressions work for you:

   range(length)[s]

or

   range(*s.indices(length))

But other users may need different behavior (if they want convert slice(-20, 
-10) to range(-20, -10)). There is no general solution which would work for 
all, you have to code what you need.

--
nosy: +rhettinger, serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46312] range() function could accept slice() objects as parameters

2022-01-09 Thread yota moteuchi


Change by yota moteuchi :


--
components: +Interpreter Core -Library (Lib)

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46312] range() function could accept slice() objects as parameters

2022-01-09 Thread yota moteuchi


Change by yota moteuchi :


--
components: +Library (Lib)

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46312] range() function could accept slice() objects as parameters

2022-01-09 Thread yota moteuchi


New submission from yota moteuchi :

This improvement proposal is close to : https://bugs.python.org/issue42956 and 
also detailed in 
https://stackoverflow.com/questions/13855288/turn-slice-into-range

to iterate over a slice, the recommended method seems to be : 

s = slice(5,100,3)
for i in range(s.stop)[s] :
# do something

but if range() accepted directly a slice, it would dramatically improve the 
readability :

s = slice(5,100,3)
for i in range(s) :
# do something

and it could be convenient, especially inside the __getitem__ property. I'll 
try to make a quick patch to test...

--
messages: 410143
nosy: yota moteuchi
priority: normal
severity: normal
status: open
title: range() function could accept slice() objects as parameters
type: enhancement
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com