[Python-Dev] Enum: subclassing?

Greg Ewing greg.ewing at canterbury.ac.nz
Thu May 2 05:07:18 CEST 2013


On 02/05/13 13:47, Steven D'Aprano wrote:
> The most obvious use-case for subclassing enums is to extend them:
>
> class Directions(Enum):
> north = 1
> east = 2
> west = 3
> south = 4
>
> class Directions3D(Directions):
> up = 5
> down = 6

It doesn't necessarily follow that subclassing is the right
mechanism for extending enums, though. If anything, you
really want to "superclass" them. Maybe

   class Directions3D(Enum, extends = Directions):
     up = 5
     down = 6

Then we could have issubclass(Directions, Directions3D)
rather than the reverse.

-- 
Greg


More information about the Python-Dev mailing list