Should 3.4 enums use UPPER_CASE_WITH_UNDERSCORES?

12,746

Solution 1

Update

The BDFL (Benevolent Dictator For Life) has spoken, and the Enum documentation has changed to reflect all upper-case member names.


The examples in the [previous] docs are lower-case primarily because one of the preexisting modules that Enum was based on used lower-case (or at least its author did ;).

My usage of enum has usually been something along the lines of:

class SomeEnum(Enum):
    ... = 1
    ... = 2
    ... = 3
globals().update(SomeEnum.__members__)

which effectively puts all the members in the module namespace.

So I would say whichever style feels more comfortable to you -- but pick a style and be consistent.

Solution 2

I think they're not UPPER_CASE because, well, it just looks weird when it is. Since you can only access the enumerations through the class (e.g. my_enum.VALUE) it looks weird if the members are capitalized. In C the members of the enumeration go into the module namespace, so it doesn't look weird (to me) when the members are capitalized, in usage:

typedef enum {OFF, ON} lightswitch;
lightswitch bathroomLight = ON;

But in Python you access them through the enumeration class that you create, and it looks weird to go from ClassStyle names to ALL_CAPS.

class Lightswitch(Enum):
    OFF = 0
    ON = 1

# isn't that weird?
my_light = Lightswitch.OFF

Bottom line, I think it's just aesthetic. I've been wrong before, though, and I realize that this is just my opinion.

Solution 3

When in doubt about style, I usually defer to the style used in standard library code and examples from the official documentation. It keeps me from wasting time on arbitrary decisions.

So in this case, I recommend lower case, like variable names.

Share:
12,746
cdonts
Author by

cdonts

Updated on June 08, 2022

Comments

  • cdonts
    cdonts almost 2 years

    As the documentation says, an enumeration is a set of symbolic names (members) bound to unique, constant values. The PEP8 says that constants are usually named as UPPER_CASE, should I use this notation in Python 3.4 enums? If yes, why the examples in the docs are using lower_case?

    • Kev
      Kev over 10 years
      In languages that support enumerations as first class citizens, Enumerations != Constants. Yes, the enumeration members may have constant values, but that's not the same as a variable being a constant.
    • mgilson
      mgilson over 10 years
      FWIW, there are lots of PEP8 infractions in the standard lib ... And the whole thing is meant to be a guide anyway. One of the most important parts of it is knowing when to bend/break the rules. IMHO, this is a bit of a corner case and I wouldn't fault you for going either way. Just be consistent.
    • Brian Campbell
      Brian Campbell over 10 years
      @mgilson One of the problems treating PEP8 merely as a guide that you might bend or break are that it means that there's a lot more room for bikeshedding on style issues. When I'm reviewing someone's code, and I come across a patch which upcases something because the author thinks that it counts as a constant while I disagree, then that turns into a whole long pointless email thread (this actually came up recently as someone upcased a global logger because Pylint complained about it). It's nice to have an authoritative, easy to apply rule to avoid this kind of bikeshedding.
    • mgilson
      mgilson over 10 years
      @BrianCampbell -- I'm not saying anything novel here. All I'm saying is that as far as I see it, depending on your interpretation of what is and isn't a constant, two different people could come to different judgments about what to do here. On a project basis, sometimes it's necessary to adopt additional style rules.
  • Ry-
    Ry- over 10 years
    Well, it’s not weirder than, say, re.IGNORECASE. (I don’t find it weird at all, really, but that’s subjective.)
  • Cody Piersall
    Cody Piersall over 10 years
    @minitech You are right; it really is subjective. Maybe the difference is that re.IGNORECASE is a module level constant, and the enum's member's look more like class attributes.
  • cdonts
    cdonts over 10 years
    Definitely it's subjective, I like the lowercase.UPPER_CASE notation. But I think I'll use lower_case just like in the docs. Thanks! +1
  • cdonts
    cdonts over 10 years
    Mmm I don't like using enums like that but it's just my opinion. I think I'll use lower_case. Thanks for the answer!
  • cdonts
    cdonts over 10 years
    Well I'll accept this but there's no right answer, all answers are good and it depends on the programmer. Thanks.
  • David Mašek
    David Mašek over 8 years
    Lightswitch.OFF is actually preffered way in Java so people comming form Java might see that as less weird that the lowercase.
  • Ethan Furman
    Ethan Furman about 7 years
    The BDFL (Benevolent Dictator For Life) has spoken, and the Enum documentation has changed to reflect all upper-case member names.
  • variable
    variable about 4 years
    What is the purpose of "globals().update(SomeEnum.__members__)"? SomeEnum is already in the modules namespace so what exactly is that line achieving?
  • Ethan Furman
    Ethan Furman about 4 years
    @variable: it's putting the members themselves in the module namespace -- so instead of SomeEnum.SOME_MEMBER you can just type SOME_MEMBER.