[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-20 Thread Steve Jorgensen
Ethan Furman wrote:
> On 7/9/22 12:19, Steve Jorgensen wrote:
> > [...] It works great to combine them by defining the dataclass as a mixin 
> > for the Enum class. Why would
> > it not be good to include that as an example in the official docs, assuming 
> > (as I believe) that it is a
> > particularly useful combination?
> > Do you have some real-world examples that show this?
> --
> ~Ethan~

I have only used it in 1 real-world case s far. It's a good use case but not a 
good example case. I'll keep using this pattern though, and I'll probably end 
up with a good example soonish.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FGK4R4ES3STAS2PZLYX5UOV5HZRIFSF2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-18 Thread Ethan Furman

On 7/9/22 12:19, Steve Jorgensen wrote:

> [...] It works great to combine them by defining the dataclass as a mixin for 
the Enum class. Why would
> it not be good to include that as an example in the official docs, assuming 
(as I believe) that it is a
> particularly useful combination?

Do you have some real-world examples that show this?

--
~Ethan~
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OSGWU6QMAN34TNQHK2ZVN5K3EPKGRGZX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-17 Thread Ethan Furman

On 7/7/22 18:22, Steve Jorgensen wrote:

> After some playing around, I figured out a pattern that works without any 
changes to the
> implementations of `dataclass` or `Enum`, and I like this because it keeps 
the 2 kinds of
> concern separate. Maybe I'll try submitting an MR to add an example like this 
to the
> documentation for `Enum`.
>
> In [1]: from dataclasses import dataclass
>
> In [2]: from enum import Enum
>
> In [3]: @dataclass(frozen=True)
> ...: class CreatureDataMixin:
> ...: size: str
> ...: legs: int
> ...:
>
> In [4]: class Creature(CreatureDataMixin, Enum):
> ...: BEETLE = ('small', 6)
> ...: DOG = ('medium', 4)
> ...:
>
> In [5]: Creature.DOG
> Out[5]: Creature(size='medium', legs=4)

I'm impressed that you found a way to make it work.  Be aware that some of the bug-fixing in 3.11 has changed the 
resulting repr() -- the above now looks like:




It would be possible to have Enum check to see if the data type is a dataclass, and then see if the repr is set to be 
automatically created:


>>> CreatureDataMixin.__dataclass_params__
_DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=True)


I'll have to look into that.

It does seem like a lot of extra work, or at least no less work, than just 
writing an `__init__` in the enum class directly.

--
~Ethan~
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/X2KBATIBRGAPSVYNAWX77WCRIPUMQKLX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-17 Thread Ethan Furman

On 7/8/22 19:50, Ethan Furman wrote:

> The repr from a combined dataclass/enum looks like a dataclass, giving no 
clue that the
> object is an enum, and omitting any information about which enum member it is 
and which
> enum it is from.

Fixed in 3.11:  ``

--
~Ethan~
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2CURYFJAFITUMBVTSJMVJSOBOVL2OXUA/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-17 Thread Ethan Furman

On 7/7/22 09:01, Steve Jorgensen wrote:

> Actually, maybe these are fundamentally incompatible? `@dataclass` is a 
decorator, so it
> acts on the class after it was already defined, but `Enum` acts before that 
when `@dataclass`
> cannot have not generated the `__init__` yet. Right?

Correct.

--
~Ethan~
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TXBQEVYMFWDIKNXU6X6IASU22G42PWFG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-17 Thread Ethan Furman

On 7/6/22 17:01, Steve Jorgensen wrote:

> Perhaps, this has already been addressed in a newer release (?) but in Python 
3.9, making
> `@dataclass` work with `Enum` is a bit awkward.
>
> Currently, it order to make it work, I have to:
> 1. Pass `init=False` to `@dataclass` and hand-write the `__init__` method
> 2. Pass `repr=False` to `@dataclass` and use `Enum`'s representation or write 
a custom __repr__
>
> Example:
> In [72]: @dataclass(frozen=True, init=False, repr=False)
>  ...: class Creature(Enum):
>  ...: legs: int
>  ...: size: str
>  ...: Beetle = (6, 'small')
>  ...: Dog = (4, 'medium')
>  ...: def __init__(self, legs, size):
>  ...: self.legs = legs
>  ...: self.size = size
>  ...:
>
> In [73]: Creature.Dog
> Out[73]: 

So why use dataclass then?

class Creature(Enum):
Beetle = (6, 'small')
Dog = (4, 'medium')
def __init__(self, legs, size):
self.legs = legs
self.size = size

and

>>> list(Creature)
[, ]

>>> Creature.Beetle.size
'small'

>>> Creature.Beetle.legs
6

It looks like dataclass was just making you do a bunch of extra work.

--
~Ethan~
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/X5UCN42LFGSGISWD62SUP2BU4WDRGWDM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-12 Thread Michael Foord
On Tue, 12 Jul 2022, 18:05 Steve Jorgensen,  wrote:

> Chris Angelico wrote:
> > On Mon, 11 Jul 2022 at 03:54, Steve Jorgensen stevec...@gmail.com wrote:
> > > David Mertz, Ph.D. wrote:
> > > I've seen this thread, and also wondered why anyone could EVER want a
> > > dataclass that is an enum.  Nothing I've seen in the thread gives me
> any
> > > hint about that, really.
> > > On Sun, Jul 10, 2022 at 7:44 AM Barry Scott ba...@barrys-emacs.org
> wrote:
> > > On 9 Jul 2022, at 22:53, Steve Jorgensen stevec...@gmail.com wrote:
> > > I don't think that dataclasses have the limited set of intended uses
> > > that you are interpreting them as having. To me, the fact that they
> can be
> > > frozen makes them a good fit with Enum.
> > > Please quote the email that you are replying to.
> > > It is usually considered a code smell to have a class that is two or
> more
> > > things.
> > > This seems to be what you are trying to do.
> > > How can one class be a set of fields and also the enum for one of its
> own
> > > fields?
> > > I do not understand why this is resonable.
> > > Barry
> > > Python-ideas mailing list -- python-ideas@python.org
> > > To unsubscribe send an email to python-ideas-le...@python.org
> > > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > > Message archived at
> > >
> https://mail.python.org/archives/list/python-ideas@python.org/message/V6U7UM.
> ..
> > > Code of Conduct: http://python.org/psf/codeofconduct/
> > > Python-ideas mailing list -- python-ideas@python.org
> > > To unsubscribe send an email to python-ideas-le...@python.org
> > > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > > Message archived at
> > >
> https://mail.python.org/archives/list/python-ideas@python.org/message/HZFZE3.
> ..
> > > Code of Conduct: http://python.org/psf/codeofconduct/
> > > --
> > > Keeping medicines from the bloodstreams of the sick; food
> > > from the bellies of the hungry; books from the hands of the
> > > uneducated; technology from the underdeveloped; and putting
> > > advocates of freedom in prisons.  Intellectual property is
> > > to the 21st century what the slave trade was to the 16th.
> > > Sorry, I don't know how I communicated that I was trying to have one
> class be a set of fields and also the enum for one of its own fields.
> > > I'm really just wanting to have each member of the enum be an instance
> of a frozen dataclass. If an of the dataclass fields were of an enum type,
> then it would presumably not be for the same enum. In my example, none of
> the fields of the dataclass contains an enum. One contains a string, and
> the other contains an int.
> > > Just throwing an idea out there, but would it work better to have an
> > enum-namedtuple instead?
> > ChrisA
>
> The only benefit I can think of for namedtuple vs a dataclass is
> compactness in memory, but the number of members of an enum is typically
> very small. I think the extra flexibility of a dataclass makes more
> desirable for this purpose.
>

The ability to unpack a namedtuple as an iterable is considered to be a
great advantage the dataclass has over the named tuple because changing the
number of members is a backwards incompatible change for namedtuple.

No reason in principle why a frozen dataclass should be less memory
efficient than a namedtuple (?).

Michael


___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/F4YM66UAQ3GXXBIMPNX6MLEQA22K7UVL/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DJU32XDG2HDHQAEQN2H5KTRABCXLWUYY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-12 Thread Steve Jorgensen
Chris Angelico wrote:
> On Mon, 11 Jul 2022 at 03:54, Steve Jorgensen stevec...@gmail.com wrote:
> > David Mertz, Ph.D. wrote:
> > I've seen this thread, and also wondered why anyone could EVER want a
> > dataclass that is an enum.  Nothing I've seen in the thread gives me any
> > hint about that, really.
> > On Sun, Jul 10, 2022 at 7:44 AM Barry Scott ba...@barrys-emacs.org wrote:
> > On 9 Jul 2022, at 22:53, Steve Jorgensen stevec...@gmail.com wrote:
> > I don't think that dataclasses have the limited set of intended uses
> > that you are interpreting them as having. To me, the fact that they can be
> > frozen makes them a good fit with Enum.
> > Please quote the email that you are replying to.
> > It is usually considered a code smell to have a class that is two or more
> > things.
> > This seems to be what you are trying to do.
> > How can one class be a set of fields and also the enum for one of its own
> > fields?
> > I do not understand why this is resonable.
> > Barry
> > Python-ideas mailing list -- python-ideas@python.org
> > To unsubscribe send an email to python-ideas-le...@python.org
> > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > Message archived at
> > https://mail.python.org/archives/list/python-ideas@python.org/message/V6U7UM...
> > Code of Conduct: http://python.org/psf/codeofconduct/
> > Python-ideas mailing list -- python-ideas@python.org
> > To unsubscribe send an email to python-ideas-le...@python.org
> > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > Message archived at
> > https://mail.python.org/archives/list/python-ideas@python.org/message/HZFZE3...
> > Code of Conduct: http://python.org/psf/codeofconduct/
> > --
> > Keeping medicines from the bloodstreams of the sick; food
> > from the bellies of the hungry; books from the hands of the
> > uneducated; technology from the underdeveloped; and putting
> > advocates of freedom in prisons.  Intellectual property is
> > to the 21st century what the slave trade was to the 16th.
> > Sorry, I don't know how I communicated that I was trying to have one class 
> > be a set of fields and also the enum for one of its own fields.
> > I'm really just wanting to have each member of the enum be an instance of a 
> > frozen dataclass. If an of the dataclass fields were of an enum type, then 
> > it would presumably not be for the same enum. In my example, none of the 
> > fields of the dataclass contains an enum. One contains a string, and the 
> > other contains an int.
> > Just throwing an idea out there, but would it work better to have an
> enum-namedtuple instead?
> ChrisA

The only benefit I can think of for namedtuple vs a dataclass is compactness in 
memory, but the number of members of an enum is typically very small. I think 
the extra flexibility of a dataclass makes more desirable for this purpose.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/F4YM66UAQ3GXXBIMPNX6MLEQA22K7UVL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-12 Thread Michael Foord
On Fri, 8 Jul 2022 at 02:22, Steve Jorgensen  wrote:

> After some playing around, I figured out a pattern that works without any
> changes to the implementations of `dataclass` or `Enum`, and I like this
> because it keeps the 2 kinds of concern separate. Maybe I'll try submitting
> an MR to add an example like this to the documentation for `Enum`.
>
> In [1]: from dataclasses import dataclass
>
> In [2]: from enum import Enum
>
> In [3]: @dataclass(frozen=True)
>...: class CreatureDataMixin:
>...: size: str
>...: legs: int
>...:
>
> In [4]: class Creature(CreatureDataMixin, Enum):
>...: BEETLE = ('small', 6)
>...: DOG = ('medium', 4)
>...:
>
> In [5]: Creature.DOG
> Out[5]: Creature(size='medium', legs=4)
>

I really like this example. I love dataclasses and I love enums and it
looks like they go together like peanut butter and chocolate.You get a free
initialiser (__init__ method) and all the other goodness of dataclasses
(which are really very good).

I tweeted your example and it got 29 likes :-)

https://twitter.com/voidspace/status/1546832332056924161

Michael




> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/G2VALQ4RIVFKIOKVW4XZAHZMLSZWL2XS/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 

Michael Foord

Python Consultant, Contractor and Trainer

https://agileabstractions.com/
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DZ73MVKZSWJE7ADUZFZ7NKJUAFPRTKWE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-10 Thread Chris Angelico
On Mon, 11 Jul 2022 at 03:54, Steve Jorgensen  wrote:
>
> David Mertz, Ph.D. wrote:
> > I've seen this thread, and also wondered why anyone could EVER want a
> > dataclass that is an enum.  Nothing I've seen in the thread gives me any
> > hint about that, really.
> > On Sun, Jul 10, 2022 at 7:44 AM Barry Scott ba...@barrys-emacs.org wrote:
> > > On 9 Jul 2022, at 22:53, Steve Jorgensen stevec...@gmail.com wrote:
> > > I don't think that dataclasses have the limited set of intended uses
> > > that you are interpreting them as having. To me, the fact that they can be
> > > frozen makes them a good fit with Enum.
> > > Please quote the email that you are replying to.
> > > It is usually considered a code smell to have a class that is two or more
> > > things.
> > > This seems to be what you are trying to do.
> > > How can one class be a set of fields and also the enum for one of its own
> > > fields?
> > > I do not understand why this is resonable.
> > > Barry
> > >
> > > Python-ideas mailing list -- python-ideas@python.org
> > > To unsubscribe send an email to python-ideas-le...@python.org
> > > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > > Message archived at
> > > https://mail.python.org/archives/list/python-ideas@python.org/message/V6U7UM...
> > > Code of Conduct: http://python.org/psf/codeofconduct/
> > >
> > > Python-ideas mailing list -- python-ideas@python.org
> > > To unsubscribe send an email to python-ideas-le...@python.org
> > > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > > Message archived at
> > > https://mail.python.org/archives/list/python-ideas@python.org/message/HZFZE3...
> > > Code of Conduct: http://python.org/psf/codeofconduct/
> > > --
> > Keeping medicines from the bloodstreams of the sick; food
> > from the bellies of the hungry; books from the hands of the
> > uneducated; technology from the underdeveloped; and putting
> > advocates of freedom in prisons.  Intellectual property is
> > to the 21st century what the slave trade was to the 16th.
>
> Sorry, I don't know how I communicated that I was trying to have one class be 
> a set of fields and also the enum for one of its own fields.
>
> I'm really just wanting to have each member of the enum be an instance of a 
> frozen dataclass. If an of the dataclass fields were of an enum type, then it 
> would presumably not be for the same enum. In my example, none of the fields 
> of the dataclass contains an enum. One contains a string, and the other 
> contains an int.
>

Just throwing an idea out there, but would it work better to have an
enum-namedtuple instead?

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YVF6OKL5UPCUWWU72R36WDIQ2KTWC5SF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-10 Thread Steve Jorgensen
David Mertz, Ph.D. wrote:
> I've seen this thread, and also wondered why anyone could EVER want a
> dataclass that is an enum.  Nothing I've seen in the thread gives me any
> hint about that, really.
> On Sun, Jul 10, 2022 at 7:44 AM Barry Scott ba...@barrys-emacs.org wrote:
> > On 9 Jul 2022, at 22:53, Steve Jorgensen stevec...@gmail.com wrote:
> > I don't think that dataclasses have the limited set of intended uses
> > that you are interpreting them as having. To me, the fact that they can be
> > frozen makes them a good fit with Enum.
> > Please quote the email that you are replying to.
> > It is usually considered a code smell to have a class that is two or more
> > things.
> > This seems to be what you are trying to do.
> > How can one class be a set of fields and also the enum for one of its own
> > fields?
> > I do not understand why this is resonable.
> > Barry
> > 
> > Python-ideas mailing list -- python-ideas@python.org
> > To unsubscribe send an email to python-ideas-le...@python.org
> > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > Message archived at
> > https://mail.python.org/archives/list/python-ideas@python.org/message/V6U7UM...
> > Code of Conduct: http://python.org/psf/codeofconduct/
> > 
> > Python-ideas mailing list -- python-ideas@python.org
> > To unsubscribe send an email to python-ideas-le...@python.org
> > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > Message archived at
> > https://mail.python.org/archives/list/python-ideas@python.org/message/HZFZE3...
> > Code of Conduct: http://python.org/psf/codeofconduct/
> > -- 
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century what the slave trade was to the 16th.

Sorry, I don't know how I communicated that I was trying to have one class be a 
set of fields and also the enum for one of its own fields.

I'm really just wanting to have each member of the enum be an instance of a 
frozen dataclass. If an of the dataclass fields were of an enum type, then it 
would presumably not be for the same enum. In my example, none of the fields of 
the dataclass contains an enum. One contains a string, and the other contains 
an int.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KWL2FXQ2FKRMGBAB5PMR3GIRAQBC6CLR/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-10 Thread David Mertz, Ph.D.
I've seen this thread, and also wondered why anyone could EVER want a
dataclass that is an enum.  Nothing I've seen in the thread gives me any
hint about that, really.

On Sun, Jul 10, 2022 at 7:44 AM Barry Scott  wrote:

>
>
> > On 9 Jul 2022, at 22:53, Steve Jorgensen  wrote:
> >
> > I don't think that dataclasses have the limited set of intended uses
> that you are interpreting them as having. To me, the fact that they can be
> frozen makes them a good fit with Enum.
>
> Please quote the email that you are replying to.
>
> It is usually considered a code smell to have a class that is two or more
> things.
> This seems to be what you are trying to do.
>
> How can one class be a set of fields and also the enum for one of its own
> fields?
> I do not understand why this is resonable.
>
> Barry
>
>
>
> > ___
> > Python-ideas mailing list -- python-ideas@python.org
> > To unsubscribe send an email to python-ideas-le...@python.org
> > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/V6U7UMQRTLDZ2W6SWREL472L6ZH7MHB5/
> > Code of Conduct: http://python.org/psf/codeofconduct/
> >
>
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/HZFZE3YMCMT6CF5VZHNM6ZJVDAX6LZXK/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HMYCB2RWSKK5SVKO2O2NF7OSHD4KZTR3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-10 Thread Barry Scott



> On 9 Jul 2022, at 22:53, Steve Jorgensen  wrote:
> 
> I don't think that dataclasses have the limited set of intended uses that you 
> are interpreting them as having. To me, the fact that they can be frozen 
> makes them a good fit with Enum.

Please quote the email that you are replying to.

It is usually considered a code smell to have a class that is two or more 
things.
This seems to be what you are trying to do.

How can one class be a set of fields and also the enum for one of its own 
fields?
I do not understand why this is resonable.

Barry



> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/V6U7UMQRTLDZ2W6SWREL472L6ZH7MHB5/
> Code of Conduct: http://python.org/psf/codeofconduct/
> 

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HZFZE3YMCMT6CF5VZHNM6ZJVDAX6LZXK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-09 Thread Steve Jorgensen
I don't think that dataclasses have the limited set of intended uses that you 
are interpreting them as having. To me, the fact that they can be frozen makes 
them a good fit with Enum.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/V6U7UMQRTLDZ2W6SWREL472L6ZH7MHB5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-09 Thread Steve Jorgensen
Ethan Furman wrote:
> On 7/7/22 09:01, Steve Jorgensen wrote:
> > Actually, maybe these are fundamentally incompatible?
> > Their intended use seems fundamentally incompatible:
> - dataclass was designed for making many mutable records (hundreds, 
> thousands, or more)
> - enum was designed to make a handful of named constants (I haven't yet seen 
> one with even a hundred elements)
> The repr from a combined dataclass/enum looks like a dataclass, giving no 
> clue that the object is an enum, and omitting 
> any information about which enum member it is and which enum it is from.
> Given these conflicts of interest, I don't see any dataclass examples making 
> it into the enum documentation.
> --
> ~Ethan~

Per my subsequent self-reply, they are only incompatible when trying to do them 
at the same time in the same class definition. It works great to combine them 
by defining the dataclass as a mixin for the Enum class. Why would it not be 
good to include that as an example in the official docs, assuming (as I 
believe) that it is a particularly useful combination?
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VFGXT4QOWYF3UJVWYOR54GNTKEG2XT7D/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-08 Thread Ethan Furman

On 7/7/22 09:01, Steve Jorgensen wrote:

> Actually, maybe these are fundamentally incompatible?

Their intended use seems fundamentally incompatible:

- dataclass was designed for making many mutable records (hundreds, thousands, 
or more)
- enum was designed to make a handful of named constants (I haven't yet seen 
one with even a hundred elements)

The repr from a combined dataclass/enum looks like a dataclass, giving no clue that the object is an enum, and omitting 
any information about which enum member it is and which enum it is from.


Given these conflicts of interest, I don't see any dataclass examples making it 
into the enum documentation.

--
~Ethan~
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6CIPPTP2Z42GVUIVOUXZ7BW46DB5HWEZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-08 Thread Barry Scott



> On 8 Jul 2022, at 02:22, Steve Jorgensen  wrote:
> 
> After some playing around, I figured out a pattern that works without any 
> changes to the implementations of `dataclass` or `Enum`, and I like this 
> because it keeps the 2 kinds of concern separate. Maybe I'll try submitting 
> an MR to add an example like this to the documentation for `Enum`.
> 
> In [1]: from dataclasses import dataclass
> 
> In [2]: from enum import Enum
> 
> In [3]: @dataclass(frozen=True)
>   ...: class CreatureDataMixin:
>   ...: size: str
>   ...: legs: int
>   ...: 
> 
> In [4]: class Creature(CreatureDataMixin, Enum):
>   ...: BEETLE = ('small', 6)
>   ...: DOG = ('medium', 4)
>   ...: 
> 
> In [5]: Creature.DOG
> Out[5]: Creature(size='medium', legs=4)

Can't you define the type of size as an enum?
Using multiple inheritance seems like the wrong way to go.
What if you are 10 fields in the dataclass that are all enums?
That could get messy.

Disclaimer I have not used dataclass. Just thinking from OOD point of view.

Barry

> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/G2VALQ4RIVFKIOKVW4XZAHZMLSZWL2XS/
> Code of Conduct: http://python.org/psf/codeofconduct/
> 

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/CBH24S2XG5EF2CQWER6BWLBSZEGK4Q7O/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-07 Thread Steve Jorgensen
After some playing around, I figured out a pattern that works without any 
changes to the implementations of `dataclass` or `Enum`, and I like this 
because it keeps the 2 kinds of concern separate. Maybe I'll try submitting an 
MR to add an example like this to the documentation for `Enum`.

In [1]: from dataclasses import dataclass

In [2]: from enum import Enum

In [3]: @dataclass(frozen=True)
   ...: class CreatureDataMixin:
   ...: size: str
   ...: legs: int
   ...: 

In [4]: class Creature(CreatureDataMixin, Enum):
   ...: BEETLE = ('small', 6)
   ...: DOG = ('medium', 4)
   ...: 

In [5]: Creature.DOG
Out[5]: Creature(size='medium', legs=4)
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/G2VALQ4RIVFKIOKVW4XZAHZMLSZWL2XS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-07 Thread Steve Jorgensen
Steve Jorgensen wrote:
> Perhaps, this has already been addressed in a newer release (?) but in Python 
> 3.9, making `@dataclass` work with `Enum` is a bit awkward.
> Currently, it order to make it work, I have to:
> 1. Pass `init=False` to `@dataclass` and hand-write the `__init__` method
> 2. Pass `repr=False` to `@dataclass` and use `Enum`'s representation or write 
> a custom __repr__
> Example:
> In [72]: @dataclass(frozen=True, init=False, repr=False)
> ...: class Creature(Enum):
> ...: legs: int
> ...: size: str
> ...: Beetle = (6, 'small')
> ...: Dog = (4, 'medium')
> ...: def __init__(self, legs, size):
> ...: self.legs = legs
> ...: self.size = size
> ...:
> In [73]: Creature.Dog
> Out[73]: 

Actually, maybe these are fundamentally incompatible? `@dataclass` is a 
decorator, so it acts on the class after it was already defined, but `Enum` 
acts before that when `@dataclass` cannot have not generated the `__init__` 
yet. Right?
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/T775WMOLR6TNOXDAU37ZA2FKQB3SMJT6/
Code of Conduct: http://python.org/psf/codeofconduct/