Re: [Python-ideas] Range and slice syntax

2018-11-13 Thread Chris Angelico
On Wed, Nov 14, 2018 at 5:14 AM David Allemang wrote: > > That is not what slice.indices does. Per help(slice.indices) - > > "S.indices(len) -> (start, stop, stride) > > "Assuming a sequence of length len, calculate the start and stop indices, and > the stride length of the extended slice

Re: [Python-ideas] Range and slice syntax

2018-11-13 Thread David Allemang
That is not what slice.indices does. Per help(slice.indices) - "S.indices(len) -> (start, stop, stride) "Assuming a sequence of length len, calculate the start and stop indices, and the stride length of the extended slice described by S. Out of bounds indices are clipped in a manner consistent

Re: [Python-ideas] Range and slice syntax

2018-11-13 Thread Vladimir Filipović
On Mon, Nov 12, 2018 at 4:43 PM Nicholas Harrison wrote: > Only when this is called (implicitly or explicitly) do checks for valid > objects and bounds occur. From my experience using slices, this is how they > work in that context too. On reconsideration, I've found one more argument in

Re: [Python-ideas] Range and slice syntax

2018-11-12 Thread Steven D'Aprano
On Mon, Nov 12, 2018 at 11:23:21AM -0500, David Mertz wrote: > >>> import pandas as pd > >>> import numpy as np > >>> I = pd.IndexSlice > >>> J = np.s_ > >>> I[4:10:3] > slice(4, 10, 3) I'm not entirely sure that I like the idea of conflating slice *constructor* with slice *usage*. Slice

Re: [Python-ideas] Range and slice syntax

2018-11-12 Thread Nicholas Harrison
For sake of completeness, here is another possible problem I've found with it. I was afraid of making something context-dependent, and therefore breaking its consistency. Here is the use of slices in the current language that breaks my rules: my_array[:,2] # valid syntax, though I've typically

Re: [Python-ideas] Range and slice syntax

2018-11-12 Thread David Mertz
I mostly like the abstraction being proposed, but the syntactical edge cases like `[::3]` (infinite list crashes) and {4:10} (a dict not a slice/range set) tip the balance against it for me. Saying to add some various stars and parens in various not-really-obvious places just makes the proposal

Re: [Python-ideas] Range and slice syntax

2018-11-12 Thread Nicholas Harrison
That's true. I should clarify what I was thinking a bit more. Maybe it's better to say that the new syntax creates a slice object: (::) # this creates slice(None, None, None) It accepts any object into its arguments and they default to None when they are left off. This can be passed into list

Re: [Python-ideas] Range and slice syntax

2018-11-12 Thread Nicholas Harrison
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 wrote: > I'm wondering how your examples would go with from funcoperators import > infix (https://pypi.org/project/funcoperators/) > > sum(1:6) #

Re: [Python-ideas] Range and slice syntax

2018-11-12 Thread Nicholas Harrison
Overall, I agree with you. It is more intuitive to an experienced Python user, and not so helpful to beginners. It decreases the ability to read out code like English sentences and makes it harder to know what to search for online. So it boosts facility after you know the language, but not when

Re: [Python-ideas] Range and slice syntax

2018-11-12 Thread Nicholas Harrison
That's a good point. It might be better to disallow the list and set versions all together. To get a list or set you would instead have to explicitly unpack a range/slice object: [*(:5)] # [:5] no longer allowed {*(1:6)} # {1:6} is a dict That would also solve the misstep of the

Re: [Python-ideas] Range and slice syntax

2018-11-11 Thread Steven D'Aprano
On Sun, Nov 11, 2018 at 04:34:16PM +, Juancarlo Añez wrote: > On Sun, Nov 11, 2018 at 6:00 AM Chris Angelico wrote: > > > Be careful of this last one. If you omit the step, it looks like this: > > > > {start:stop} > > > > which is a dictionary display. > > > > The parenthesis could always

Re: [Python-ideas] Range and slice syntax

2018-11-11 Thread Juancarlo Añez
On Sun, Nov 11, 2018 at 6:00 AM Chris Angelico wrote: > Be careful of this last one. If you omit the step, it looks like this: > > {start:stop} > > which is a dictionary display. > The parenthesis could always be required for this new syntax. In [*1*]: {'a':1} Out[*1*]: {'a': 1} In [*2*]:

Re: [Python-ideas] Range and slice syntax

2018-11-11 Thread Vladimir Filipović
On Sun, Nov 11, 2018 at 6:59 AM Nicholas Harrison wrote: > Any of the values may be omitted and in the slice context the behavior has no > changes from what it already does: start and stop default to the beginning or > end of the list depending on direction and the step defaults to 1. Just to

Re: [Python-ideas] Range and slice syntax

2018-11-11 Thread Robert Vanden Eynde
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

Re: [Python-ideas] Range and slice syntax

2018-11-11 Thread Steven D'Aprano
On Sat, Nov 10, 2018 at 10:58:02PM -0700, Nicholas Harrison wrote: [...] > (start:stop:step) > > > Meet a range/slice object. Parentheses are required. (Its syntax in this > regard follows exactly the same rules as a generator expression.) I say > both range and slice because it can be used in

Re: [Python-ideas] Range and slice syntax

2018-11-10 Thread Chris Angelico
On Sun, Nov 11, 2018 at 4:59 PM Nicholas Harrison wrote: > It has a couple of siblings that should be obvious (think list or set > comprehension): > > [start:stop:step] # gives a list > {start:stop:step} # gives a set > Be careful of this last one. If you omit the step, it looks like this: