[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
R. David Murray
rdmurray at bitdance.com
Tue Apr 23 16:53:54 CEST 2013
On Tue, 23 Apr 2013 16:33:02 +0200, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Le Tue, 23 Apr 2013 10:24:18 -0400,
> "R. David Murray" <rdmurray at bitdance.com> a écrit :
> > >
> > > I'm having a problem with the proposed implementation. I haven't
> > > found any mention of it, so apologies if this has already been
> > > discussed:
> > >
> > > >>> from flufl.enum import *
> > > >>> class C(Enum):
> > > ... a = 1
> > > ... b = 2
> > > ...
> > > >>> C.a.__class__
> > > <class 'flufl.enum._enum.EnumValue'>
> > > >>> isinstance(C.a, C)
> > > False
> > > >>> isinstance(C(1), C)
> > > False
> > >
> > > It would really be better if instances were actual instances of the
> > > class, IMO.
> >
> > The first False looks correct to me, I would not expect an enum value
> > to be an instance of the class that holds it as an attribute. The
> > second certainly looks odd, but what does it even mean to have an
> > instance of an Enum class?
>
> Perhaps I should have added some context:
>
> >>> class C(Enum):
> ... a = 1
> ... b = 2
> ...
> >>> C(1)
> <EnumValue: C.a [value=1]>
> >>> C[1]
> <EnumValue: C.a [value=1]>
>
> It is simply the same as a __getattr__ call.
>
> That said, I don't see why it wouldn't make sense for an enum value to
> be an instance of that class. It can be useful to write
> `isinstance(value, MyEnumClass)`. Also, any debug facility which has a
> preference for writing out class names would produce a better output
> than the generic "EnumValue".
Ah. I'd be looking for a bug every time I saw isinstance(value,
myEnumClass). A better class name for values and an API for
getting that class from the EnumClass would be nice, though.
(So that you could write "isinstance(value, MyEnumClass.ValueClass)",
say.)
--David
More information about the Python-Dev
mailing list