[Python-Dev] Enum Eccentricities

Guido van Rossum guido at python.org
Mon Sep 23 04:37:49 CEST 2013


On Sun, Sep 22, 2013 at 4:52 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> http://bugs.python.org/**issue19011 <http://bugs.python.org/issue19011>
>
> So, as anyone who has worked with Enum is probably aware by now, Enum's
> are a strange duck -- you might even call them platypuses.
>
> For example, Enum members are instances of the class, but they are defined
> inside the class structure, and new ones cannot be created afterwards; Enum
> classes also support iteration and containment checks.
>
> What I'm looking for feedback on is the question is #19011: should an Enum
> member be considered a class level attribute?
>
> On the one hand, they are defined inside the class, and they are accessed
> via dot notation (EnumClass.member).
>
> On the other hand, inside the class they look like 3 and 5 and 'up' and
> 'east', and they don't live in the class dictionary.
>
> Also, if we change Enum so that members do act more like class attributes,
> then things like Color.red.blue.green.blue will result in Color.blue, and
> that seems stranger to me than having class instances be available on the
> class without be full-fledged class-attributes.
>
> Thoughts?  Opinions?  Pearls of wisdom?
>

I wouldn't lose much sleep over this. Classes can override __getattribute__
so that instance variables appear to exist even though they are not in the
instance's __dict__. It's the same for metaclasses.

As for attributes appearing different inside the class than when accessed
as an attribute, that's not unusual -- descriptors can legitimately do
that. It was more common in Python 2, where this applied to all unbound
methods, but even in Python 3 it applies to static and class methods.

I would draw the line at being able to access members as attributes of
other members. Making Color.red.blue be a spelling for Color.blue feels
like an abomination. If you can make dir(Color) return the strings 'red',
'blue' etc. in addition to other class attributes without making
Color.red.blue work, go for it. If you can't, that's fine too. Users should
(obviously) steer clear from relying on either behavior.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130922/80bf98a6/attachment.html>


More information about the Python-Dev mailing list