[Python-Dev] Enum: subclassing?

Steven D'Aprano steve at pearwood.info
Thu May 2 03:47:25 CEST 2013


On 02/05/13 06:45, Antoine Pitrou wrote:

> I was talking in the context where subclassing is allowed. I don't
> think there's a use-case for subclassing of non-empty enums. On the
> other hand, empty enums should probably allow subclassing (they are
> "abstract base enums", in a way).

If you google for "subclassing enums" you will find many people asking
how to subclass enums.

Apparently Apache's Java allows subclassing, if I'm reading this correctly:

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/enums/Enum.html

So do Scala and Kotlin.

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


If you allow enums to have methods, then the most obvious use-case is to add or extend methods, no different to any other class.



-- 
Steven


More information about the Python-Dev mailing list