Should nested classes in an Enum be Enum members?

Ethan Furman ethan at stoneleaf.us
Wed Jun 27 10:48:53 EDT 2018


[Note:  there is a similar thread on Python-Ideas, if you would like to respond there.]

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~



More information about the Python-list mailing list