[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Steven D'Aprano
On Fri, Jul 10, 2020 at 06:06:03PM +0100, Stestagg wrote: > I don't believe that this feature would steepen the language learning curve > however, but actually help to shallow it slightly (Explained more below) It steepens it because it's yet another feature to learn. Is it dicts or dict views

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Inada Naoki
I'm -1 too. Making ordered containers to sequence-like only for random.choice is a wrong idea. If random.choice should support non-sequence ordered container, just propose it to random.choice. And if the proposal was rejected, you can do it by yourself with helper functions. >>> import random

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread David Mertz
Definitely -1 after reading all the discussion. The strongest argument I've seen is: `list(d.items())` adds six characters. So far, there's absolutely nothing described that cannot easily be accomplished with list(). Moreover, even apart from the work of maintaining the feature itself, the

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Stestagg
> I too have sometimes proposed what I think of as "minor quality-of-life" > enhancements, and had them shot down. It stings a bit, and can be > frustrating, but remember it's not personal. > I don't mind the shooting down, as long as the arguments make sense :D. It seems like we're both in

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Paul Moore
On Fri, 10 Jul 2020 at 17:45, Steven D'Aprano wrote: > I must admit I like the look of this, but I don't know what I would use > it for. It feels very much like the sort of "here's some syntax that might match someone's mental model of something" that is common in languages that focus on

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Chris Angelico
On Sat, Jul 11, 2020 at 12:45 AM Steven D'Aprano wrote: > > On Fri, Jul 10, 2020 at 02:50:17PM +1000, Chris Angelico wrote: > > > And immediately above that part, I said that I had made use of this, > > and had used it in Python by listifying the dict first. Okay, so I > > didn't actually dig up

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Steven D'Aprano
On Fri, Jul 10, 2020 at 02:48:31PM +0200, Alex Hall wrote: > I believe he was saying it would be weird if `d[1, 2]` called > `d.__getitem__((1, 2))` but `d[1, 2, x=3]` called `d.__getitem__(1, 2, > x=3)`. More generally, if `__getitem__` always receives a single positional > argument now, it

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Steven D'Aprano
On Fri, Jul 10, 2020 at 01:59:06PM +0100, Jonathan Fine wrote: > I wrote: Let's proceed. We continue to use d = Dummy(). Given that >>>> key = d[1, 2, 3, a=4, b=5] > is allowed, what should we be able to say about the key. Clearly it should > be an instance of a class and there should be

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Jonathan Fine
Hi Ricky Thank you for your very helpful comment, which Paul has usefully amplified. You wrote: This is confusing to me because `my_mapping[1,2,3]` is already valid syntax > equivalent to `my_mapping.__setitem__(my_mapping, (1,2,3))`. Wouldn't a > change to translate the tuple into this K object

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Ronald Oussoren via Python-ideas
> On 10 Jul 2020, at 15:38, Eric V. Smith wrote: > > > On 7/10/2020 9:20 AM, Paul Moore wrote: >> On Fri, 10 Jul 2020 at 13:47, Stestagg wrote: >> >>> The intent of my statement was: The current implementation of dict does not >>> allow any reasonable implementation of

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Paul Moore
On Fri, 10 Jul 2020 at 15:23, Jonathan Fine wrote: > With hindsight, I can see your difficulty. The original idea is to extend the > syntax so that > >>> m = MyMap() > >>> m[1, 2, 3, a=4, b=5] = 'foobar' > is allowed in some future version of Python. > > Python's syntax already allows: >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Steven D'Aprano
On Fri, Jul 10, 2020 at 02:50:17PM +1000, Chris Angelico wrote: > And immediately above that part, I said that I had made use of this, > and had used it in Python by listifying the dict first. Okay, so I > didn't actually dig up the code where I'd done this, but that's a use > case. I have

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Ricky Teachey
> Let's assume MyMap does have a __setitem__ method. Extending what you > pointed out, the assignment > >>> m[K(1, 2, 3, a=4, b=5)] = 'foobar' > will then be equivalent to > >>> MyMap.__setitem__(m, k, 'foobar') > where > >>> k = K(1, 2, 3, a=4, b=5) > > This is confusing to me

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Steven D'Aprano
On Thu, Jul 09, 2020 at 06:26:41PM +0100, Stestagg wrote: > As for use-cases, I'll admit that I see this as a fairly minor > quality-of-life issue. Thank you for that comment. I too have sometimes proposed what I think of as "minor quality-of-life" enhancements, and had them shot down. It

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Jonathan Fine
Hi Paul I'm writing in response to one of your comments, all of which are useful. Please forgive me, Paul, for not responding to the others in this message. I wrote: > Next, the implementation of such K-mappings. Here, K.set and K.get > decorators will help. Something like (not tested): >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Eric V. Smith
On 7/10/2020 9:20 AM, Paul Moore wrote: On Fri, 10 Jul 2020 at 13:47, Stestagg wrote: The intent of my statement was: The current implementation of dict does not allow any reasonable implementation of dict_keys/dict_values/dict_items with O(1) index behaviour. I.e. the performance

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Paul Moore
On Fri, 10 Jul 2020 at 14:26, Stestagg wrote: > > All valid points, I'd recommend catching up on the entire thread, it'll be a > lot quicker than re-iterating them here. I have been reading the thread (admittedly skimming). I've seen nothing yet to suggest that there's a useful change to Python

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Paul Moore
On Fri, 10 Jul 2020 at 11:54, Jonathan Fine wrote: > Let's proceed. We continue to use d = Dummy(). Given that >>>> key = d[1, 2, 3, a=4, b=5] > is allowed, what should we be able to say about the key. Clearly it should be > an instance of a class, and there should be a way of creating

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Stestagg
All valid points, I'd recommend catching up on the entire thread, it'll be a lot quicker than re-iterating them here. The highly condensed tl;dr version is that list(d.items())[n] is sometimes the fastest option, but sometimes not, it's not always faster (sometimes many thousands of times slowe)

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Paul Moore
On Fri, 10 Jul 2020 at 13:47, Stestagg wrote: > The intent of my statement was: The current implementation of dict does not > allow any reasonable implementation of dict_keys/dict_values/dict_items with > O(1) index behaviour. I.e. the performance scalability of any future changes > to the

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Jonathan Fine
Hi I will clarify my previous post, by making a stronger statement. Every Python object is an instance of a class. I wrote: Let's proceed. We continue to use d = Dummy(). Given that >>> key = d[1, 2, 3, a=4, b=5] is allowed, what should we be able to say about the key. Clearly it should

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Alex Hall
On Fri, Jul 10, 2020 at 2:33 PM Steven D'Aprano wrote: > On Fri, Jul 10, 2020 at 11:52:19AM +0100, Jonathan Fine wrote: > > > FUTURE > > Let's proceed. We continue to use d = Dummy(). Given that > >>>> key = d[1, 2, 3, a=4, b=5] > > is allowed, what should we be able to say about the

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Stestagg
On Fri, Jul 10, 2020 at 1:28 PM Ronald Oussoren wrote: > > > On 10 Jul 2020, at 14:08, Stestagg wrote: > > >> Adding indexing to views adds another requirement to the dict >> implementation: >> > > Yes, that's the proposed change > > >> indexing for sequences at least suggests that access is

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Steven D'Aprano
On Fri, Jul 10, 2020 at 08:23:09AM -0400, Ricky Teachey wrote: > My mind instantly went to the idea of using this syntax as a way write > single line mathematical function definitions: > > f[x, y] = x + y This won't work, because the right hand side will be evaluated first. The above is legal

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Joao S. O. Bueno
I believe that one of the most popular Python domains that benefit from "abusing" indexes is data analysis in the numpy/Pandas world. I am not familiar enough with Pandas to make useful speculation on how named indexes could enhance the usage of dataframes, - maybe someone more familiar can

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Steven D'Aprano
On Fri, Jul 10, 2020 at 11:52:19AM +0100, Jonathan Fine wrote: > FUTURE > Let's proceed. We continue to use d = Dummy(). Given that >>>> key = d[1, 2, 3, a=4, b=5] > is allowed, what should we be able to say about the key. Clearly it should > be an instance of a class That's not clear at

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Ronald Oussoren via Python-ideas
> On 10 Jul 2020, at 14:08, Stestagg wrote: > > > Adding indexing to views adds another requirement to the dict implementation: > > Yes, that's the proposed change > > indexing for sequences at least suggests that access is O(1). > That makes it impossible to use, as an example, a linked

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Ricky Teachey
On Fri, Jul 10, 2020, 6:54 AM Jonathan Fine wrote: > Hi All > > SUMMARY > This is a longish post. It looks at the idea in general terms, and > outlines a way to get the desired semantics (by not syntax) with Python as > it is today. And this would be forward compatible with the new syntax, if >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Stestagg
> > > Adding indexing to views adds another requirement to the dict > implementation: > Yes, that's the proposed change > indexing for sequences at least suggests that access is O(1). > That makes it impossible to use, as an example, a linked list to preserve > insertion order. > The docs for

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Ronald Oussoren via Python-ideas
> On 10 Jul 2020, at 05:33, Christopher Barker wrote: > > On Thu, Jul 9, 2020 at 10:29 AM Stestagg > wrote: > The idea that there are future, unspecified changes to dicts() that may or > may not be hampered by allowing indexing sounds like FUD to me, unless there

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Jonathan Fine
Hi All SUMMARY This is a longish post. It looks at the idea in general terms, and outlines a way to get the desired semantics (by not syntax) with Python as it is today. And this would be forward compatible with the new syntax, if provided later. PRESENT I like the idea of allowing >>> d[1,

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Neil Girdhar
What the future of this? I looked at type annotations in networkx recently ( https://github.com/networkx/networkx/pull/4014), and I wanted to keep things simple, so I proposed and implemented Graph[NodeType] However, I knew that they may ultimately want Graph[NodeType, EdgeTypedDict,

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Oscar Benjamin
On Fri, 10 Jul 2020 at 06:56, Chris Angelico wrote: > > On Fri, Jul 10, 2020 at 3:41 PM Inada Naoki wrote: > > > > On Fri, Jul 10, 2020 at 1:53 PM Chris Angelico wrote: > > > > > > > > > And immediately above that part, I said that I had made use of this, > > > and had used it in Python by

[Python-ideas] Re: PEP 472 -- Support for indexing with keyword arguments

2020-07-10 Thread Stefano Borini
I am one of the authors of the PEP. My problem was to deal with natural notation in quantum chemistry mostly. It had no technical purpose, but I still think it would open some interesting options. The PEP was rejected mostly because of lack of interest. On Mon, 4 May 2020 at 00:07, Andras Tantos

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Inada Naoki
Oh, sorry. I read you did "random pick from dict", but I hadn't read you did "random pick and mutate dict" use case or not. On Fri, Jul 10, 2020 at 3:08 PM Chris Angelico wrote: > > On Fri, Jul 10, 2020 at 4:04 PM Inada Naoki wrote: > > > > On Fri, Jul 10, 2020 at 2:56 PM Chris Angelico wrote:

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Chris Angelico
On Fri, Jul 10, 2020 at 4:04 PM Inada Naoki wrote: > > On Fri, Jul 10, 2020 at 2:56 PM Chris Angelico wrote: > > > > but it might be > > mutated in between. So the list would have to be reconstructed fresh > > every time. > > > > Do you think the use case is common enough to add a feature to

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Inada Naoki
On Fri, Jul 10, 2020 at 2:56 PM Chris Angelico wrote: > > but it might be > mutated in between. So the list would have to be reconstructed fresh > every time. > Do you think the use case is common enough to add a feature to builtin type? I can not imagine. Anyway, if you want to benchmark that