[Python-Dev] enum discussion: can someone please summarize open issues?

Georg Brandl g.brandl at gmx.net
Mon Apr 29 07:03:27 CEST 2013


Am 28.04.2013 22:36, schrieb Ethan Furman:

>>> Example enumeration:
>>> 
>>> class Seasons(Enum): SPRING = 1 SUMMER = 2 AUTUMN = 3 WINTER = 4
>>> 
>>> days_in_year = 365
>>> 
>>> @property def avg_temp(self): return (75, 92, 66, 33)[int(self)+1] #
>>> enums are 1-based
>>> 
>>> 
>>> Definite Issues:
>>> 
>>> - should enum items be of the type of the Enum class? (i.e. type(SPRING) 
>>> is Seasons)
>> 
>> IMO Yes.
> 
> I agree.
> 
>>> - should an enum item be selectable via __call__ instead of __getitem__ 
>>> (i.e. Seasons(3) is AUTUMN)
>> 
>> No opinion.
> 
> I think the callable syntax should be supported for database integration and
> consistency with every (?) other type in Python.  No opinion about the
> __getitem__ portion.
> 
> 
>>> - should days_in_year be enumerated?
>> 
>> Yes. (If you don't want it to be, and it's not a method/descriptor, move it
>> out of the class.)
> 
> Making it a property to have it in the class certainly works for me.
> 
> 
>>> - should avg_temp be enumerated?
>> 
>> IMO No.
> 
> I agree.
> 
> 
>>> - for the above two, how should they be included/excluded?
>> 
>> IMO Everything should be enumerated except (a) things with a __get__()
>> method (i.e. descriptors) (b) __dunder__ names
> 
> This also works for me.
> 
> 
>> Also, I believe there's still an issue on the order in which items are 
>> returned by iter(Seasons), but I don't know which way this is heading.
> 
> As somebody pointed out earlier, the only order which cannot be reconstructed
> after the fact is definition order (value order can be, lexical order can be,
> etc.).  So my vote is to have the default iteration order be the original
> definition order, as any other desired order can be added to the class.

Thanks for the summary, that was very helpful.  I find myself agreeing with
every one of your opinions.  Good job :)

Georg




More information about the Python-Dev mailing list