[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-10 Thread Andrew Barnert via Python-ideas
On Apr 10, 2020, at 15:53, Steele Farnsworth wrote: > > I have implemented a class in the C code of the collections module which has > similar behavior to the defaultdict class. This class, dynamicdict, supplies > values for keys that have not yet been added using a default factory > callable,

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-10 Thread David Mertz
I have often wanted this! I use defaultdict only occasionally, and every time I do, I start out thinking it behaves the way you describe dynamicdict. Then I read the docs again and decide I didn't actually want defaultdict to start with, and roll something different. On Fri, Apr 10, 2020, 6:50 PM

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-10 Thread Steven D'Aprano
On Fri, Apr 10, 2020 at 06:02:25PM -0700, Andrew Barnert via Python-ideas wrote: > (Keep in mind that defaultdict > was added somewhere around 2.4 or 2.5, while __missing__ has only been > there since somewhere around 2.7/3.3. I’ll bet it would be different > if it were invented today.) Both

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-13 Thread Caleb Donovick
I have built this data structure countless times. So I am in favor. > Why can’t you just subclass dict and override that? Because TypeError: multiple bases have instance lay-out conflict is one of my least favorite errors. Perhaps `__missing__` could be a first class part of the getitem of proto

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-14 Thread Andrew Barnert via Python-ideas
> On Apr 13, 2020, at 18:44, Caleb Donovick wrote: >  > I have built this data structure countless times. So I am in favor. Maybe you can give a concrete example of what you need it for, then? I think that would really help the proposal. Especially if your example needs a per-instance rather t

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-14 Thread Steele Farnsworth
I've implemented the class as a stand-alone module here: https://github.com/swfarnsworth/dynamicdict It could in theory be made significantly more concise if `defdict_type` were the base for this class instead of `PyDict_Type`. On Tue, Apr 14, 2020 at 1:32 PM Andrew Barnert via Python-ideas < p

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-15 Thread Caleb Donovick
> Besides performance, I don’t think it fits with Guido’s conception of the protocols as being more minimal than the builtin types—e.g., set has not just a & operator, but also an intersection method that takes 0 or more arbitrary iterables; the set protocol has no such method, so collections.abc.S

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-15 Thread Andrew Barnert via Python-ideas
>> On Apr 15, 2020, at 14:08, Caleb Donovick wrote: >> >> Besides performance, I don’t think it fits with Guido’s conception of the >> protocols as being more minimal than the builtin types—e.g., set has not >> just a & operator, but also an intersection method that takes 0 or more >> arbitrar

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-15 Thread Steven D'Aprano
On Mon, Apr 13, 2020 at 06:43:53PM -0700, Caleb Donovick wrote: > > Why can’t you just subclass dict and override that? > > Because TypeError: multiple bases have instance lay-out conflict is one of > my least favorite errors. For the benefit of the people on this list who aren't as familiar wit

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-16 Thread Caleb Donovick
> construction calls __init__ if __new__ returns an instance Actually type's __call__ method does that, although that doesn't help my point at all... Your point about there being no method to perform the dispatch is good. To get what I want without interpreter changes there would need to be som

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-16 Thread Andrew Barnert via Python-ideas
On Apr 16, 2020, at 11:03, Caleb Donovick wrote: > > Or alternatively MissingMapping (or some other mixin) could define > __getitem__ and require __getitem_impl__ and __missing__. As this wouldn't > require any changes to anything I think it might be the best solution I have > proposed. I t

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-16 Thread Andrew Barnert via Python-ideas
On Apr 16, 2020, at 12:26, Andrew Barnert wrote: > > Somewhere I have some code for a set of class decorators that help > implementing mappings and sequences: you provide basic __fooitem__ methods, > and it wraps them with methods that do all the extra stuff dict, tuple, and > list do. IIRC, t

[Python-ideas] Re: Proposed class for collections: dynamicdict

2020-04-17 Thread Jeff Edwards
So, going back to the original post, dynamicdict is definitely something I've reimplemented myself multiple times essentially exactly as your pseudo-code in your C-command describes, just with the factory being required to be not None (because I explicitly didn't want it to accept the 'no-factory'