[Python-Dev] Dataclasses, frozen and __post_init__

2018-02-22 Thread Jim J. Jewett
On Mon, Feb 19, 2018 at 5:06 PM, Chris Barker - NOAA Federal < chris.barker at noaa.gov> wrote: > If I have this right, on the discussion about frozen and hash, a use > case was brought up for taking a few steps to create an instance (and > thus wanting it not frozen) and then wanting it hashable.

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Greg Ewing
Chris Barker - NOAA Federal wrote: If I have this right, on the discussion about frozen and hash, a use case was brought up for taking a few steps to create an instance (and thus wanting it not frozen) and then wanting it hashable. Which pointed to the idea of a “ freeze this from now on” method

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Greg Ewing
Steven D'Aprano wrote: So in principle, we could have a mutable class, and a immutable one, and when you flick the switch, the instance.__class__ changes from mutable to frozen. That seems unfriendly to subclasses as well. To extend a class you now need to subclass both the mutable and immutab

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Raymond Hettinger
> On Feb 20, 2018, at 2:38 PM, Guido van Rossum wrote: > > But then the class would also inherit a bunch of misfeatures from tuple (like > being indexable and having a length). It would be nicer if it used __slots__ > instead. FWIW, George Sakkis made a tool like this about nine years ago.

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Eric V. Smith
> On Feb 20, 2018, at 5:38 PM, Guido van Rossum wrote: > >> On Tue, Feb 20, 2018 at 1:37 PM, Eric V. Smith wrote: >>> On 2/17/2018 2:35 PM, Guido van Rossum wrote: >>> PS. I have to ponder why frozen dataclasses don't use `__new__`. >> >> As I'm sure everyone is now aware after the rest of thi

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Guido van Rossum
On Tue, Feb 20, 2018 at 1:37 PM, Eric V. Smith wrote: > On 2/17/2018 2:35 PM, Guido van Rossum wrote: > >> PS. I have to ponder why frozen dataclasses don't use `__new__`. >> > > As I'm sure everyone is now aware after the rest of this discussion: it's > because the returned object isn't really i

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Guido van Rossum
On Tue, Feb 20, 2018 at 12:50 PM, Chris Barker wrote: > Is that one attribute that big a deal? I suppose that simple dataclasses > with only two or so attributes would see a significant change, but if > you're really worried about space, wouldn't you use a namedtuple or simply > a tuple anyway? >

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Eric V. Smith
On 2/17/2018 2:35 PM, Guido van Rossum wrote: PS. I have to ponder why frozen dataclasses don't use `__new__`. As I'm sure everyone is now aware after the rest of this discussion: it's because the returned object isn't really immutable. That said, I have threatened to create a decorator simi

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Chris Barker
On Tue, Feb 20, 2018 at 8:45 AM, Guido van Rossum wrote: > TBH, I don't hate Nick's solution that assigns to __class__ in > __post_init__. You can probably come up with a class decorator that hides > construction of the frozen subclass. I do think that it should be used very > sparingly, as the e

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Guido van Rossum
On Mon, Feb 19, 2018 at 8:16 PM, Glenn Linderman wrote: > On 2/19/2018 7:02 PM, Guido van Rossum wrote: > > But how? > > On Mon, Feb 19, 2018 at 5:06 PM, Chris Barker - NOAA Federal < > chris.bar...@noaa.gov> wrote: > >> ... maybe it would be helpful to be able to >> freeze an instance after crea

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-20 Thread Nick Coghlan
On 20 February 2018 at 15:11, Steven D'Aprano wrote: > So in principle, we could have a mutable class, and a immutable one, and > when you flick the switch, the instance.__class__ changes from mutable > to frozen. > > If you don't hate this, we can think about the details needed to get > it work i

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-19 Thread Steven D'Aprano
On Mon, Feb 19, 2018 at 07:02:42PM -0800, Guido van Rossum wrote: > On Mon, Feb 19, 2018 at 5:06 PM, Chris Barker - NOAA Federal < > chris.bar...@noaa.gov> wrote: [...] > > This seems another use case — maybe it would be helpful to be able to > > freeze an instance after creation for multiple use-

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-19 Thread Glenn Linderman
On 2/19/2018 7:02 PM, Guido van Rossum wrote: But how? On Mon, Feb 19, 2018 at 5:06 PM, Chris Barker - NOAA Federal mailto:chris.bar...@noaa.gov>> wrote: ... maybe it would be helpful to be able to freeze an instance after creation for multiple use-cases? And there's the crux of th

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-19 Thread Chris Barker - NOAA Federal
But how? Well, I hadn’t thought that far ;-) But it would make frozen an instance level property, rather than a class-level one — some instances would be frozen, some not. Which would be kinda compatible with the idea of hashability being a property of values, rather than type. Frozen-ness woul

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-19 Thread Guido van Rossum
But how? On Mon, Feb 19, 2018 at 5:06 PM, Chris Barker - NOAA Federal < chris.bar...@noaa.gov> wrote: > If I have this right, on the discussion about frozen and hash, a use > case was brought up for taking a few steps to create an instance (and > thus wanting it not frozen) and then wanting it ha

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-19 Thread Chris Barker - NOAA Federal
If I have this right, on the discussion about frozen and hash, a use case was brought up for taking a few steps to create an instance (and thus wanting it not frozen) and then wanting it hashable. Which pointed to the idea of a “ freeze this from now on” method. This seems another use case — mayb

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-18 Thread Guido van Rossum
The underlying issue here is that we don't want an extra state flag in the object to indicate "this object is currently [im]mutable". Using __class__ assignment to signal this is clever way to add this state, though not without risks. On Sun, Feb 18, 2018 at 4:34 PM, Nick Coghlan wrote: > On 18

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-18 Thread Nick Coghlan
On 18 February 2018 at 14:10, Guido van Rossum wrote: > Agreed the __pre_init__ idea is no improvement. I think we're back where you > started -- just use `object.__setattr__` to set the attribute in > `__post_init__`. That's what the PEP says is used by the generated > `__init__`, so I think it i

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-17 Thread Guido van Rossum
Agreed the __pre_init__ idea is no improvement. I think we're back where you started -- just use `object.__setattr__` to set the attribute in `__post_init__`. That's what the PEP says is used by the generated `__init__`, so I think it is reasonable to copy that pattern. Presumably the situation doe

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-17 Thread Ben Lewis
> > Why can'y you make `name` on `NamedObjectItem` a property that returns ` > self.obj.name`? Why store a duplicate copy of the name? > Agreed, it's probably a better design not to store a duplicate reference to name. But when I tried that, the property clashed with the inherited field. This caus

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-17 Thread Guido van Rossum
Why can'y you make `name` on `NamedObjectItem` a property that returns ` self.obj.name`? Why store a duplicate copy of the name? PS. I have to ponder why frozen dataclasses don't use `__new__`. On Fri, Feb 16, 2018 at 11:43 PM, Ben Lewis wrote: > On Sat, Feb 17, 2018 at 6:40 PM, Guido van Rossu

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-17 Thread Ben Lewis
On Sat, Feb 17, 2018 at 6:40 PM, Guido van Rossum wrote: > > > That's a pretty tricky proposal, and one that's been debated on and off > for a long time in other contexts. And that flag would somehow have to be > part of every instance's state. > > In general the right way to initialize an immuta

Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-16 Thread Guido van Rossum
On Fri, Feb 16, 2018 at 3:38 PM, Ben Lewis wrote: > I have been using dataclasses package in a pet project of mine. I'm sorry > if this issue has already been raised. I came across a situation where I > wanted to use the __post_init__ function to initialise some inherited > fields from a dataclas

[Python-Dev] Dataclasses, frozen and __post_init__

2018-02-16 Thread Ben Lewis
Hello I have been using dataclasses package in a pet project of mine. I'm sorry if this issue has already been raised. I came across a situation where I wanted to use the __post_init__ function to initialise some inherited fields from a dataclass with frozen=True. The problem is that because it is