Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-18 Thread Chris Barker via Python-ideas
On Thu, Oct 18, 2018 at 10:12 AM, Sven R. Kunze wrote: > On 18.10.18 18:49, Anders Hovmöller wrote: > >> If it's AND, shouldn't it be "hasinterfaces" (notice the s!)? > > yeah, that would make sense. Is someone proposing something here? The point I was making, is in the case of ABCs: issubclass

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-18 Thread Anders Hovmöller
> Does AND even make sense for isinstance/issubclass? Why wouldn't it? Python supports multiple inheritance. / Anders ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-18 Thread Sven R. Kunze
On 18.10.18 18:49, Anders Hovmöller wrote: If it's AND, shouldn't it be "hasinterfaces" (notice the s!)? Yeah, could be. To be sure, we are on the same page here: "interface" refers to a set of attributes of the object in question, does it? E.g. like the __iter__ iterface. I usually don't c

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-18 Thread Anders Hovmöller
>> Even though, it would be the same as issubclass() (though I'd like an AND >> relationship with the tuple of ABCs..) > > When "hasinterface" ANDs the tuple, it's already different, isn't it? > If it's AND, shouldn't it be "hasinterfaces" (notice the s!)? One could also imagine that isinstan

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-18 Thread Sven R. Kunze
On 12.10.18 01:30, Chris Barker via Python-ideas wrote: If Python had a way to check ABCs without issubclass, then I wouldn't care about subclasses at all. I'd actually kind of like to have: hasinterface(an_object, (ABC1, ABC2, ABC3)) I actually like your idea and could imagine using "hasint

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread Philip Martin
I think it could inherit from the Mapping abc? class frozendict(Mapping): def __new__(cls, *args, **kwargs): d = dict(*args, **kwargs) proxy = MappingProxyType(d) instance = super().__new__(cls) instance.__proxy = proxy return instance def __hash__

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread Zaur Shibzukhov
вторник, 16 октября 2018 г., 12:29:55 UTC+3 пользователь Steven D'Aprano написал: > > > > It seems to me that we would need this restriction to make a reasonably > > universal frozendict that is, itself, hashable. > > When people talk about frozendicts being hashable, they mean it in the > sa

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread Steven D'Aprano
On Tue, Oct 16, 2018 at 01:02:03AM -0700, George Leslie-Waksman wrote: > Would a frozendict require that keys and values be hashable? Keys, yes. Values, no. If the values were hashable, the frozendict itself would also be hashable. If not, then it would be like trying to hash a tuple with unhas

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread Chris Angelico
On Tue, Oct 16, 2018 at 7:02 PM George Leslie-Waksman wrote: > > Would a frozendict require that keys and values be hashable? Keys would already have to be hashable - regular dicts demand this, and there's no reason not to for frozendict. Values? Not so sure. Personally I would say that no, they

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-16 Thread George Leslie-Waksman
Would a frozendict require that keys and values be hashable? It seems to me that we would need this restriction to make a reasonably universal frozendict that is, itself, hashable. With this restriction for the general case, is there still sufficient value for everyone that is asking for a frozend

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-12 Thread Greg Ewing
Chris Barker - NOAA Federal via Python-ideas wrote: Or maybe come up with a new name We should call it a birdseyedict, because of this: http://www.vulture.com/2016/12/unearthing-a-rare-1971-monty-python-film-all-about-peas.html -- Greg ___ Python-i

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Chris Barker via Python-ideas
On Thu, Oct 11, 2018 at 3:35 PM, Steven D'Aprano wrote: > On Thu, Oct 11, 2018 at 12:34:13PM -0700, Chris Barker via Python-ideas > wrote: > > > I don't care what is or isn't a subclass of what -- I don't think that's > a > > Pythonic question. But I do think : > > > > issubclass(frozendict, abc.

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Chris Angelico
On Fri, Oct 12, 2018 at 9:16 AM Steven D'Aprano wrote: > > On Fri, Oct 12, 2018 at 02:45:30AM +1100, Chris Angelico wrote: > > On Fri, Oct 12, 2018 at 2:41 AM Chris Barker - NOAA Federal via > > Python-ideas wrote: > > > > > > > This violates the Liskov Substitution Principle. > > > > > > If we R

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Steven D'Aprano
On Thu, Oct 11, 2018 at 12:34:13PM -0700, Chris Barker via Python-ideas wrote: > I don't care what is or isn't a subclass of what -- I don't think that's a > Pythonic question. But I do think : > > issubclass(frozendict, abc.Mapping) and issubclass(frozendict, abc.Hashable) > > would be useful.

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Steven D'Aprano
On Fri, Oct 12, 2018 at 02:45:30AM +1100, Chris Angelico wrote: > On Fri, Oct 12, 2018 at 2:41 AM Chris Barker - NOAA Federal via > Python-ideas wrote: > > > > > This violates the Liskov Substitution Principle. > > > > If we REALLY had a time machine, then dict would subclass frozendict, > > and w

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Chris Barker via Python-ideas
BTW: In [7]: issubclass(set, frozenset) Out[7]: False In [8]: issubclass(frozenset, set) Out[8]: False no reason to do anything different here. and: In [13]: issubclass(MappingProxyType, abc.Hashable) Out[13]: False so yes, there is a need for something different -CHB On Thu, Oct 11, 201

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Chris Barker via Python-ideas
On Thu, Oct 11, 2018 at 9:54 AM, Jonathan Fine wrote: > Summary: Long post. Because of LSP, neither dict nor frozendict are a > subclass of the other. given Python's dynamic typing, these issues are kinda academic :-) > Intuition tells me that a frozen dictionary is a form of dictionary > > th

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Jonathan Fine
A link on https://en.wikipedia.org/wiki/Liskov_substitution_principle goes to http://www.engr.mun.ca/~theo/Courses/sd/5895-downloads/sd-principles-3.ppt.pdf which has a very nice example (slides 14 and 15). Slide 14: Is immutable Square a behavioural subtype of immutable Rectangle? And vice versa?

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Jonathan Fine
Summary: Long post. Because of LSP, neither dict nor frozendict are a subclass of the other. Chris Barker wrote: > If we REALLY had a time machine, then dict would subclass frozendict, > and we’d be all set. Prediction is difficult, particularly when it involves the future. - Neils Bohr. And be

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Chris Angelico
On Fri, Oct 12, 2018 at 2:41 AM Chris Barker - NOAA Federal via Python-ideas wrote: > > > This violates the Liskov Substitution Principle. > > If we REALLY had a time machine, then dict would subclass frozendict, > and we’d be all set. Thanks to virtual subclassing, we can still do this. The ques

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Chris Barker - NOAA Federal via Python-ideas
> This violates the Liskov Substitution Principle. If we REALLY had a time machine, then dict would subclass frozendict, and we’d be all set. But to what extent do we need to support ALL the ways to check for an interface? Personally, I think EAFTP is the most “Pythonic”, but if folks want to ch

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Jonathan Fine
> https://en.wikipedia.org/wiki/Liskov_substitution_principle > https://en.wikipedia.org/wiki/Don%27t_repeat_yourself I did an internet search for: python liskov (over the past year). The first result was a Ruby page (but principle probably the same) https://www.netguru.co/codestories/solid-princ

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Jonathan Fine
Steve D'Aprano wrote > Zaur Shibzukhov wrote > > class frozendict(dict): > This violates the Liskov Substitution Principle. > https://en.wikipedia.org/wiki/Liskov_substitution_principle and so, Steve said, we should not make frozendict a subclass of dict. Perhaps Zaur was thinking of Don't Repea

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Steven D'Aprano
On Thu, Oct 11, 2018 at 02:18:52AM -0700, Zaur Shibzukhov wrote: > > May be the following simple prototype of frozendict could be useful? > > def frozen_error(): >return RuntimeError("frozendict is not mutable") > > class frozendict(dict): This violates the Liskov Substitution Principle.

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-11 Thread Zaur Shibzukhov
May be the following simple prototype of frozendict could be useful? def frozen_error(): return RuntimeError("frozendict is not mutable") class frozendict(dict): # def __setitem__(self, key, val): raise frozen_error() # def setdefault(self, key, val=None): rais

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Steven D'Aprano
On Thu, Oct 11, 2018 at 01:27:50PM +1100, Chris Angelico wrote: [...] [Cameron Simpson] > > Well, if it were called frozendict, indeed not. It should act like dict. > > > > So: > > > > def frozendict(**kw): > > return MappingProxyType(kw) > > > > You could make an argument for that (or a

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Serhiy Storchaka
11.10.18 07:20, João Santos пише: One important difference between MappingProxyType and a "proper" frozendict, as analog to frozenset, is that MappingProxyType doesn't have any method to return mutated versions of itself. MappingProxyType.copy()? __

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Anders Hovmöller
In tri.struct we have a class Frozen https://github.com/TriOptima/tri.struct/blob/master/lib/tri/struct/__init__.py that can be used to freeze stuff. I think something like this would be even better in the standard library, especially now with data classes! If we had this frozendict would just

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread João Santos
One important difference between MappingProxyType and a "proper" frozendict, as analog to frozenset, is that MappingProxyType doesn't have any method to return mutated versions of itself. On Thu, 11 Oct 2018 at 01:24, Steven D'Aprano wrote: > Hi Philiip, and welcome, > > On Wed, Oct 10, 2018 at

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Philip Martin
That is interesting. From my recollection, when OrderedDict was reimplemented in C, there was advice on the thread to not implement it as a subclass of dict. https://bugs.python.org/issue16991 I'm far from the right person to comment on the exact reasons, but perhaps frozenset being decoupled fro

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Philip Martin
Cameron, That's a good suggestion. Ultimately, if there are not enough various use cases for a frozendict class, I think we could add something like this as an example recipe similar to the recipe section in itertools. I would be hesitant to add a quick shim to the standard library as I can't thin

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Chris Angelico
On Thu, Oct 11, 2018 at 1:02 PM Cameron Simpson wrote: > > On 10Oct2018 20:25, Philip Martin wrote: > >Steven, that's a great idea, and I would be 100% up for your suggestion to > >have types.MappingProxyType renamed to frozendict. > > I'm not for the rename, myself. Though I'd not be against a f

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Cameron Simpson
On 10Oct2018 20:25, Philip Martin wrote: Steven, that's a great idea, and I would be 100% up for your suggestion to have types.MappingProxyType renamed to frozendict. I'm not for the rename, myself. Though I'd not be against a frozendict factory in builtins, a tiny shim for MappingProxyType.

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Philip Martin
It would help over using a regular dict as a default argument to a function by preventing accidental mutation of the default or constant mapping. This is a quickly contrived example of the convert_price function now having a side effect by changing the translation_map. from unicodedata import norm

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Philip Martin
Steven, that's a great idea, and I would be 100% up for your suggestion to have types.MappingProxyType renamed to frozendict. However, the differences in the behavior of MappingProxyType's constructor versus dict's would make the API's behavior confusing IMO. For example, MappingProxyType(x=5, y=10

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Steven D'Aprano
Hi Philiip, and welcome, On Wed, Oct 10, 2018 at 12:04:48PM -0500, Philip Martin wrote: > I generally have used MappingProxyType as a way to set default mapping > to a function or to set an empty mapping to a function. > I've created a gist with an example use case: > > https://gist.github.com

Re: [Python-ideas] Revisiting Immutable Mappings

2018-10-10 Thread Michael Selik
How does a frozendict help in that example? It's not obvious to me. Despite not understanding that example, I'm +1 for having a frozendict. I don't think it'll increase cognitive load much, as it'll sit right next to frozenset when someone reads the builtins in alphabetical order. In my own experi