[Python-Dev] Enum: subclassing?

Ethan Furman ethan at stoneleaf.us
Wed May 1 19:21:30 CEST 2013


We may not want to /completely/ disallow subclassing.  Consider:

--> class StrEnum(str, Enum):
...    '''string enums for Business Basic variable names'''
...
--> class Vendors(StrEnum):
EnumError: subclassing not allowed


My point is that IntEnum, StrEnum, ListEnum, FloatEnum are all "subclasses" of Enum.  To then have a subclass of
that, such as Season(StrEnum), is subclassing a subclass.

Now, if we do want to completely disallow it, we can ditch IntEnum and force the user to always specify the mixin
type:

--> class Season(str, Enum):
          .
          .
          .

--> class Names(str, Enum):
          .
          .
          .

But that's not very user friendly... although it's not too bad, either.

One consequence of the way it is now (IntEnum, StrEnum, etc., are allowed) is that one can put methods and other 
non-Enum item in a base class and then inherit from that for actual implemented Enum classes.

--> class StrEnum(str, Enum):
...     def describe(self):
...         print("Hi!  I'm a %s widget!" % self.value)
...

--> class Season(StrEnum):
...     spring = 'green'
...     summer = 'brown'
...     autumn = 'red'
...     winter = 'white'
...

--> class Planet(StrEnum):
...     mars = 'red'
...     earth = 'blue'
...

--> Season.summer.descripbe()
Hi!  I'm a brown widget!

--> Planet.earth.describe()
Hi!  I'm a blue widget!

--
~Ethan~


More information about the Python-Dev mailing list