Should nested classes in an Enum be Enum members?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jun 29 01:52:36 EDT 2018


On Thu, 28 Jun 2018 18:33:31 -0700, Ethan Furman wrote:

> On 06/28/2018 05:58 PM, Ben Finney wrote:
> 
>> So I remain dumbfounded as to why anyone would want a class to *both*
>> be an enumerated type, *and* have callable attributes in its API.
> 
> Perhaps I am using Enum incorrectly, but here is my FederalHoliday Enum.
>  Note that date(), next_business_day, and year() are all callables.  The
> AutoEnum parent assigns values from 1 to n for each member.  It's at
> Stackoverflow [1] if you'd like up- or down-vote it.  ;)

It isn't clear to me why FederalHoliday is an Enum, especially as the API 
seems extremely baraque. 


> class FederalHoliday(AutoEnum):
>      NewYear = "First day of the year.", 'absolute', Month.JANUARY, 1
>      MartinLutherKingJr = "Birth of Civil Rights leader.", \
>          'relative', Month.JANUARY, Weekday.MONDAY, 3 
...


I think I get the idea... the first field is a description, the second 
tells us what month the holiday is in (hope you don't have to deal with 
movable holidays which can change months...). I'm not quite sure what the 
"absolute/relative" flags are (or why they are strings instead of enums). 
Possibly they determine whether the next field is treated as an ordinal 
(first, second, third...) or numerical (1, 2, 3...) value. 

But what isn't clear to me is why these holidays are *enums*. They're 
obviously date instances, with a rich API (at least three methods) and an 
extremely complex constructor (one with variable numbers of arguments).

What makes them enums? Under what circumstances would you be comparing 
something to MartinLutherKingJr (Day) without caring about a *specific* 
Martin Luther King Jr Day?




-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list