[Python-Dev] Enum: subclassing?

Eli Bendersky eliben at gmail.com
Wed May 1 22:57:11 CEST 2013


On Wed, May 1, 2013 at 1:45 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:

> On Wed, 1 May 2013 13:43:22 -0700
> Eli Bendersky <eliben at gmail.com> wrote:
>
> > On Wed, May 1, 2013 at 1:33 PM, Antoine Pitrou <solipsis at pitrou.net>
> wrote:
> >
> > > On Wed, 1 May 2013 13:05:53 -0700
> > > Eli Bendersky <eliben at gmail.com> wrote:
> > > > On Wed, May 1, 2013 at 11:59 AM, Georg Brandl <g.brandl at gmx.net>
> wrote:
> > > >
> > > > > Am 01.05.2013 20:44, schrieb Antoine Pitrou:
> > > > > > On Wed, 01 May 2013 10:21:30 -0700
> > > > > > Ethan Furman <ethan at stoneleaf.us> wrote:
> > > > > >> 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
> > > > > >
> > > > > > I don't see the point of disallowing subclassing. It sounds like
> > > > > > a pointless restriction.
> > > > > >
> > > > > > However, perhaps the constructor should forbid the returning of a
> > > base
> > > > > > type, e.g.:
> > > > > >
> > > > > > class Season(Enum):
> > > > > >     spring = 1
> > > > > >
> > > > > > class MySeason(Season):
> > > > > >     """I look nicer than Season"""
> > > > > >
> > > > > > MySeason('spring')
> > > > > > ...
> > > > > > ValueError: Season.spring is not a MySeason instance
> > > > > >
> > > > > > (what this means is perhaps the subclassing of non-empty enum
> classes
> > > > > > should be forbidden)
> > > > >
> > > > > That's exactly what's implemented in the ref435 code at the moment.
> > > > >
> > > > >
> > > > It can't be because __call__ is by-value lookup, not by-name lookup.
> > >
> > > Ok, I've mixed up the example. But, still, since Season(1) should
> > > return the Season.spring singleton, I don't see any reasonable thing
> > > for MySeason(1) to return. Hence the request to raise an exception.
> > >
> >
> > What do you need MySeason for, though? IIUC, you don't ask to allow
> adding
> > enum values in it, so it only leaves adding extra functionality
> (methods)?
> > What are the use cases?
>
> 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).
>

I still don't understand what you mean, sorry. Like, this:

class MyEmptyEnum(Enum):
  pass

Why would you want to subclass MyEmptyEnum ?

Or do you mean this:

class IntEnum(int, Enum):
  pass

Now I can have:

class SocketFamily(IntEnum):
  ??

If it's the latter, then why allow subclassing explicitly just for this
reason? I think the explicit approach of:

class SocketFamily(int, Enum):

Is cleaner anyway, and absolves us of providing yet another enum class to
export from the stdlib.

Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130501/049d6523/attachment-0001.html>


More information about the Python-Dev mailing list