Should nested classes in an Enum be Enum members?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Jun 28 21:12:25 EDT 2018
On Thu, 28 Jun 2018 20:34:58 +1000, Ben Finney wrote:
> 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.
The PEP gives an example of enumerated members that themselves have
methods.
https://www.python.org/dev/peps/pep-0435/
There was another example somewhere (I don't remember where) of an
enumeration of the planets, where planets can have attributes and methods:
Planet.MARS.mass
Planet.JUPITER.moons()
which is no more weird than this:
class Planet(Enum):
MARS = "the red planet"
Planet.MARS.upper()
--
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson
More information about the Python-list
mailing list