Creating ranges with ellipsis
This might be a silly idea but, would it be a good idea to have ...[a:b:c] return a range(a, b, c)?
On Wed, Feb 16, 2022 at 09:44:07AM -0300, Soni L. wrote:
This might be a silly idea but, would it be a good idea to have ...[a:b:c] return a range(a, b, c)?
Similar ideas have been suggested before: https://mail.python.org/archives/list/python-ideas@python.org/thread/W44PPBJ... https://bugs.python.org/issue42956 What benefit do you see in writing [a:b:c] instead of range(a, b, c)? -- Steve
On 2022-02-16 10:45, Steven D'Aprano wrote:
On Wed, Feb 16, 2022 at 09:44:07AM -0300, Soni L. wrote:
This might be a silly idea but, would it be a good idea to have ...[a:b:c] return a range(a, b, c)?
Similar ideas have been suggested before:
https://mail.python.org/archives/list/python-ideas@python.org/thread/W44PPBJ...
https://bugs.python.org/issue42956
What benefit do you see in writing [a:b:c] instead of range(a, b, c)?
*nod* we see. syntax constructs like these are mostly about taste. it's like being able to write generators in function calls like list(x for x in foo), but also having (x for x in foo) instead of using a gen(x for x in foo) function.
16.02.22 14:44, Soni L. пише:
This might be a silly idea but, would it be a good idea to have ...[a:b:c] return a range(a, b, c)?
See PEP 204. https://www.python.org/dev/peps/pep-0204/
This might be a silly idea but, would it be a good idea to have ...[a:b:c] return a range(a, b, c)?
This sort of highly-subjective syntactic sugar makes me wonder whether there would be support for a standard python preprocessor, like what was suggested in PEP 638 [1]. [1]: https://www.python.org/dev/peps/pep-0638/ - DLD
This might be a silly idea but, would it be a good idea to have ...[a:b:c] return a range(a, b, c)?
If a 'thunderscore' is acceptable: import itertools class _ranger: @classmethod def __getitem__(self, key: slice): if isinstance(key, slice): if key.stop is None: return itertools.count(key.start, key.step or 1) return range(key.start, key.stop, key.step or 1) return range(key) ___ = _ranger() Trying to write it brings out lots of questions like what would [:y] do, or [:], [::z], etc. Only [x], [x:y], [x:], [x::z], [x:y:z] seem to make sense.
participants (6)
-
David Lowry-Duda
-
Nick Timkovich
-
Serhiy Storchaka
-
Soni L.
-
Steven D'Aprano
-
Vishesh Mangla