[Python-ideas] Range and slice syntax

Nicholas Harrison nicholasharrison222 at gmail.com
Mon Nov 12 10:25:22 EST 2018


Interesting. I haven't looked at that package before. It looks like it
would work well for that.

On Sun, Nov 11, 2018 at 4:48 AM Robert Vanden Eynde <robertve92 at gmail.com>
wrote:

> I'm wondering how your examples would go with from funcoperators import
> infix (https://pypi.org/project/funcoperators/)
>
> sum(1:6) # instead of sum(range(1, 6))
>>
>>
> sum(1 /exclusive/ 6)
>
> list(1:6)
>>
>>
> list(1 /exclusive/ 6)
> set(1 /exclusive/ 1)
>
> Note that you can pick another name.
> Note that you can pick another function :
>
> @infix
> def inclusive (a, b):
>    return range(a, b+1)
>
> sum(1 /inclusive/ 6)
>
> for i in (1:6):
>>
>> print(i**2)
>>
>>
> for i in 1 /exclusive/ 6:
>     print(i**2)
>
> (i**2 for i in (1:6))
>>
>>
> (i ** 2 for i in 1 /exclusive/ 6)
>
> It also makes forming reusable slices clearer and easier:
>>
>> my_slice = (:6:2) # instead of slice(None, 6, 2)
>> my_list[my_slice]
>>
>>
> I don't have exact equivalent here, I would create a function or
> explicitly say slice(0, 6, 2)
>
> This is similar to passing a range/slice object into the respective
>> constructor:
>>
>>
>> [1:6] # list(1:6) or [1, 2, 3, 4, 5]
>> {1:6} # set(1:6) or {1, 2, 3, 4, 5}
>>
>>
> As mentioned before {1:6} is a dict.
>
> Here are a few more examples:
>>
>>
>> for i in (:5): # 5 elements 0 to 4, i.e. range(5)
>>
>> print(i**2)
>>
>>
> Everybody knows i in range(5).
>
>
>>  for i in (1:): # counts up from one for as long as you want, i.e.
>> count(1)
>>
>>
> Well, count(1) is nice and people can google it.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181112/4d5664fb/attachment-0001.html>


More information about the Python-ideas mailing list