Should nested classes in an Enum be Enum members?

Ethan Furman ethan at stoneleaf.us
Fri Jun 29 13:36:45 EDT 2018


On 06/28/2018 10:52 PM, Steven D'Aprano wrote:
> On Thu, 28 Jun 2018 18:33:31 -0700, Ethan Furman wrote:

>> 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.

Huh.  I had to look that word up, and I still don't know what you meant exactly, although I suspect it wasn't 
complimentary.  ;-)

>> 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.

- description
- type (absolute or relative)
- month
- if absolute:
   - date
- else:
   - week day
   - which one

> 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).

They are the list of dates in which US banks are closed for electronic business (funds transfers and things).

> 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?

Enums are also useful when the underlying value is relevant; the usual example is communicating with other languages' 
APIs.  While those relevant values may have no intrinsic value, why is it a problem if they do?

--
~Ethan~



More information about the Python-list mailing list