osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Should nested classes in an Enum be Enum members?


On Fri, 29 Jun 2018 10:36:45 -0700, Ethan Furman wrote:

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

But surely the underlying value of holidays *is* something we care about: 
the date it falls. We don't just care about "Martin Luther King Jr Day" 
as an abstraction, we do care about the specific day it falls, we care 
about its relationship to other days:

- what's the last business day before MKKJ Day?
- what's the next business day after it?
- what's the date of it this year?
- will it ever fall on my birthday?

etc. I think that once we start adding methods to enums, we're starting 
to move into a gray area where they aren't *really* arbitrary enumerated 
values any longer. (Maybe.) If your methods use "self", then they 
implicitly care about the value of the enum.

I think it's a matter of degree. Past a certain level of complexity, the 
object isn't an enum any more. I don't have a problem with the "planets" 
example for enums. (Maybe I should?) But your example seems to have 
crossed that line for me.

At one extreme, we have pure symbols, or atoms, where the values don't 
support any significant operations apart from such trivial operations 
such as a human-readable string representation.

Examples in Python include None, Ellipsis and NotImplemented.

https://en.wikipedia.org/wiki/Symbol_%28programming%29


On the other extreme, we have rich, complex data types which support many 
non-trivial operations, like dicts, tuples, floats and web server 
objects. These typically (but not always) support a notion of equality 
beyond just identity.

I think enums naturally belong very close to the Symbol end of the 
continuum, and I can't help but feel your Federal Holiday example is 
sufficiently far from that end that using Enum feels uncomfortable to me.


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