Should nested classes in an Enum be Enum members?

Ben Finney ben+python at benfinney.id.au
Thu Jun 28 06:34:58 EDT 2018


Ethan Furman <ethan at stoneleaf.us> writes:

> 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

That dumbfounds my intuitions.

Specifically, I can't make sense of why someone would want to have a
class that is simultaneously behaving as an enumerated type, *and* has
an API of custom callable attributes.

> Question:
>
>   Should `SomeClass` be an enum member?  When would it be useful to
>   have an embedded class in an Enum be an enum member?

I can't think of a satisfactory answer to the question “Why is SomeClass
defined inside that enumerated type at all?”

> So I'm asking the community:  What real-world examples can you offer
> for either behavior?

That set is empty.

I'd be going straight to the author of that code; or, if that weren't an
option, re-factoring that code at the next opportunity.

-- 
 \         “Our urge to trust our senses overpowers what our measuring |
  `\         devices tell us about the actual nature of reality.” —Ann |
_o__)                                           Druyan, _Cosmos_, 2014 |
Ben Finney




More information about the Python-list mailing list