Re: [Python-ideas] Contraction for "for x in range()"
On 15 February 2017 at 00:41, Nick Timkovich wrote: > Make some shim object that you can index into to get that functionality, > could even call it Z (for the set of all integers). Short, and requires no > new syntax. > > class IndexableRange: > def __getitem__(self, item): > if isinstance(item, slice): > start = item.start if item.start is not None else 0 > step = item.step if item.step is not None else 1 > if item.stop is None: > return itertools.count(start, step) > else: > return range(start, item.stop, step) > else: > return item > > Z = IndexableRange() > > for y in Z[0:10:2]: > print(y) > > > l can, also just make function r(a,b,c) : return range(a,b,c) for example, will look similar. Initially I was by the idea to remove brackets from for statement. Now after looking more at examples I realize what the real issue with the look is. Namely it is the word "in" itself. It is simply too short and that makes a lot of space holes in the lines. And letters 'i' and 'n' sort of suck from readability POV. E.g. compare: for x in 1,10,2: and for x over 1,10,2 : So the latter actually would make it look nicer. But that would probably be even less probable to be implemented. Mikhail ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Contraction for "for x in range()"
Make some shim object that you can index into to get that functionality, could even call it Z (for the set of all integers). Short, and requires no new syntax. class IndexableRange: def __getitem__(self, item): if isinstance(item, slice): start = item.start if item.start is not None else 0 step = item.step if item.step is not None else 1 if item.stop is None: return itertools.count(start, step) else: return range(start, item.stop, step) else: return item Z = IndexableRange() for y in Z[0:10:2]: print(y) On Tue, Feb 14, 2017 at 5:22 PM, Mikhail V wrote: > > On 14 February 2017 at 22:41, MRAB wrote: > >> On 2017-02-14 21:09, Zachary Ware wrote: >> >>> On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: >>> I have a small syntax idea. In short, contraction of for x in range(a,b,c) : to for x in a,b,c : I really think there is something cute in it. So like a shortcut for range() which works only in for-in statement. So from syntactical POV, do you find it nice syntax? Visually it seems to me less bulky than range(). Example: for x in 0,5 : print (x) for y in 0,10,2 : print (y) for z in 0, y+8 : print (z) >>> >>> This is already valid and useful syntax, and thus a non-starter. >>> >>> >>> >>> The closest you could get without breaking existing code is [a:b:c]: >> >> for x in [0:5]: >> print(x) >> for y in [0:10:2]: >> print(y) >> for z in [0:y+8]: >> print(z) >> >> What's more, that could be valid outside the 'for' loop too. >> >> > This looks really good, at first glance I would even expect this will > work, > comes so common from lists and numpy index ranges. > > > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Contraction for "for x in range()"
On 14 February 2017 at 22:41, MRAB wrote: > On 2017-02-14 21:09, Zachary Ware wrote: > >> On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: >> >>> I have a small syntax idea. >>> In short, contraction of >>> >>> for x in range(a,b,c) : >>> >>> to >>> >>> for x in a,b,c : >>> >>> I really think there is something cute in it. >>> So like a shortcut for range() which works only in for-in statement. >>> So from syntactical POV, do you find it nice syntax? >>> Visually it seems to me less bulky than range(). >>> >>> Example: >>> >>> for x in 0,5 : >>> print (x) >>> for y in 0,10,2 : >>> print (y) >>> for z in 0, y+8 : >>> print (z) >>> >> >> This is already valid and useful syntax, and thus a non-starter. >> >> >> >> The closest you could get without breaking existing code is [a:b:c]: > > for x in [0:5]: > print(x) > for y in [0:10:2]: > print(y) > for z in [0:y+8]: > print(z) > > What's more, that could be valid outside the 'for' loop too. > > This looks really good, at first glance I would even expect this will work, comes so common from lists and numpy index ranges. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Contraction for "for x in range()"
for i in range(...) is *sometimes* indicative of code smell, especially when then doing x[i], though it has its uses. I've never had a need to shorten a for...range line though. Other than it being "cute", do you have an example where it's definitively better? On Tue, Feb 14, 2017 at 4:03 PM, Todd wrote: > On Tue, Feb 14, 2017 at 4:41 PM, MRAB wrote: > >> On 2017-02-14 21:09, Zachary Ware wrote: >> >>> On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: >>> I have a small syntax idea. In short, contraction of for x in range(a,b,c) : to for x in a,b,c : I really think there is something cute in it. So like a shortcut for range() which works only in for-in statement. So from syntactical POV, do you find it nice syntax? Visually it seems to me less bulky than range(). Example: for x in 0,5 : print (x) for y in 0,10,2 : print (y) for z in 0, y+8 : print (z) >>> >>> This is already valid and useful syntax, and thus a non-starter. >>> >>>>>> for x in 0,5 : >>>... print (x) >>>... for y in 0,10,2 : >>>... print (y) >>>... for z in 0, y+8 : >>>... print (z) >>>... >>>0 >>>0 >>>0 >>>8 >>>10 >>>0 >>>18 >>>2 >>>0 >>>10 >>>5 >>>0 >>>0 >>>8 >>>10 >>>0 >>>18 >>>2 >>>0 >>>10 >>> >>> The closest you could get without breaking existing code is [a:b:c]: >> >> for x in [0:5]: >> print(x) >> for y in [0:10:2]: >> print(y) >> for z in [0:y+8]: >> print(z) >> >> What's more, that could be valid outside the 'for' loop too. >> >> > Guido has already rejected this syntax and several others. > > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Contraction for "for x in range()"
On Tue, Feb 14, 2017 at 4:41 PM, MRAB wrote: > On 2017-02-14 21:09, Zachary Ware wrote: > >> On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: >> >>> I have a small syntax idea. >>> In short, contraction of >>> >>> for x in range(a,b,c) : >>> >>> to >>> >>> for x in a,b,c : >>> >>> I really think there is something cute in it. >>> So like a shortcut for range() which works only in for-in statement. >>> So from syntactical POV, do you find it nice syntax? >>> Visually it seems to me less bulky than range(). >>> >>> Example: >>> >>> for x in 0,5 : >>> print (x) >>> for y in 0,10,2 : >>> print (y) >>> for z in 0, y+8 : >>> print (z) >>> >> >> This is already valid and useful syntax, and thus a non-starter. >> >>>>> for x in 0,5 : >>... print (x) >>... for y in 0,10,2 : >>... print (y) >>... for z in 0, y+8 : >>... print (z) >>... >>0 >>0 >>0 >>8 >>10 >>0 >>18 >>2 >>0 >>10 >>5 >>0 >>0 >>8 >>10 >>0 >>18 >>2 >>0 >>10 >> >> The closest you could get without breaking existing code is [a:b:c]: > > for x in [0:5]: > print(x) > for y in [0:10:2]: > print(y) > for z in [0:y+8]: > print(z) > > What's more, that could be valid outside the 'for' loop too. > > Guido has already rejected this syntax and several others. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Contraction for "for x in range()"
On 2017-02-14 21:09, Zachary Ware wrote: On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: I have a small syntax idea. In short, contraction of for x in range(a,b,c) : to for x in a,b,c : I really think there is something cute in it. So like a shortcut for range() which works only in for-in statement. So from syntactical POV, do you find it nice syntax? Visually it seems to me less bulky than range(). Example: for x in 0,5 : print (x) for y in 0,10,2 : print (y) for z in 0, y+8 : print (z) This is already valid and useful syntax, and thus a non-starter. >>> for x in 0,5 : ... print (x) ... for y in 0,10,2 : ... print (y) ... for z in 0, y+8 : ... print (z) ... 0 0 0 8 10 0 18 2 0 10 5 0 0 8 10 0 18 2 0 10 The closest you could get without breaking existing code is [a:b:c]: for x in [0:5]: print(x) for y in [0:10:2]: print(y) for z in [0:y+8]: print(z) What's more, that could be valid outside the 'for' loop too. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Contraction for "for x in range()"
I would be surprised if this hasn't been suggested many times before. It's similar to Matlab's syntax: for x = start:step:finish end Any such change would represent a large departure from normal python syntax for dubious gain. In general, you can put any expression after the `in` keyword so long as it evaluates to an iterable or an iterator. Your specific proposal would break some perfectly valid code. You could likely come up with some symbols that couldn't show up in existing code, but it's usually better to use words instead of obscure symbol notation to preserve the readability of Python rather than letting it devolve into a concise, yet unreadable soup of symbols like Perl. On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: > I have a small syntax idea. > In short, contraction of > > for x in range(a,b,c) : > > to > > for x in a,b,c : > > I really think there is something cute in it. > So like a shortcut for range() which works only in for-in statement. > So from syntactical POV, do you find it nice syntax? > Visually it seems to me less bulky than range(). > > Example: > > for x in 0,5 : > print (x) > for y in 0,10,2 : > print (y) > for z in 0, y+8 : > print (z) > > Which would be short for: > > for x in range(0,5): > print (x) > for y in range(0,10,2): > print (y) > for z in range(0, y+8): > print (z) > > > Mikhail > > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Contraction for "for x in range()"
On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: > I have a small syntax idea. > In short, contraction of > > for x in range(a,b,c) : > > to > > for x in a,b,c : > > I really think there is something cute in it. > So like a shortcut for range() which works only in for-in statement. > So from syntactical POV, do you find it nice syntax? > Visually it seems to me less bulky than range(). > > Example: > > for x in 0,5 : > print (x) > for y in 0,10,2 : > print (y) > for z in 0, y+8 : > print (z) This is already valid and useful syntax, and thus a non-starter. >>> for x in 0,5 : ... print (x) ... for y in 0,10,2 : ... print (y) ... for z in 0, y+8 : ... print (z) ... 0 0 0 8 10 0 18 2 0 10 5 0 0 8 10 0 18 2 0 10 -- Zach ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Contraction for "for x in range()"
I have a small syntax idea. In short, contraction of for x in range(a,b,c) : to for x in a,b,c : I really think there is something cute in it. So like a shortcut for range() which works only in for-in statement. So from syntactical POV, do you find it nice syntax? Visually it seems to me less bulky than range(). Example: for x in 0,5 : print (x) for y in 0,10,2 : print (y) for z in 0, y+8 : print (z) Which would be short for: for x in range(0,5): print (x) for y in range(0,10,2): print (y) for z in range(0, y+8): print (z) Mikhail ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/