[Python-ideas] Should nested classes in an Enum be Enum members?

Elazar elazarg at gmail.com
Wed Jun 27 15:04:01 EDT 2018


People working with sum types might expect the instances of the nested
class to be instances of the enclosing class. So if the nested class is a
namedtuple, you get a sum type. The only problem is that there's no way to
express this subtype relationship in code.

Elazar

בתאריך יום ד׳, 27 ביוני 2018, 11:59, מאת Guido van Rossum ‏<guido at python.org
>:

> Sounds to me really strange that the nested class would become a member.
> Probably because everything becomes a member unless it's a function (maybe
> decorated)?
>
> On Wed, Jun 27, 2018 at 7:47 AM Ethan Furman <ethan at stoneleaf.us> wrote:
>
>> Consider the following Enum definition:
>>
>>    class Color(Enum):
>>        RED = 1
>>        GREEN = 2
>>        BLUE = 3
>>        @property
>>        def lower(self):
>>            return self.name.lower()
>>        def spam(self):
>>            return "I like %s eggs and spam!" % self.lower
>>        class SomeClass:
>>            pass
>>
>> Which of the above Color attributes are enums, and which aren't?
>>
>>
>> .
>>
>>
>> .
>>
>>
>> .
>>
>> Answer:
>>
>>    - RED, GREEN, and BLUE are members
>>
>>    - lower and spam() are not
>>
>>    - SomeClass /is/ a member (but not its instances)
>>
>>
>> Question:
>>
>>    Should `SomeClass` be an enum member?  When would it be useful to have
>> an embedded class in an Enum be an enum member?
>>
>>
>> The only example I have seen so far of nested classes in an Enum is when
>> folks want to make an Enum of Enums, and the
>> nested Enum should not itself be an enum member.  Since the
>> counter-example already works I haven't seen any requests
>> for it.  ;)
>>
>> So I'm asking the community:  What real-world examples can you offer for
>> either behavior?  Cases where nested classes
>> should be enum members, and cases where nested classes should not be
>> members.
>>
>> Thanks!
>>
>> --
>> ~Ethan~
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> --
> --Guido van Rossum (python.org/~guido)
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180627/20266714/attachment.html>


More information about the Python-ideas mailing list