Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should 3.4 enums use UPPER_CASE_WITH_UNDERSCORES?

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?

like image 690
cdonts Avatar asked Jan 26 '14 03:01

cdonts


People also ask

Should enums always be caps?

Enums are a type and the enum name should start with a capital. Enum members are constants and their text should be all-uppercase.

Should enums be camel case?

classes and enums are written in camel case and they are likewise constants. But classes and enums are not values (constant or otherwise); they're types. (Or type constructors, if generic.) So if anything, that's an argument for making constants different from those.

Should enums be all caps C#?

The only thing that you should make all caps like that are constant/final variables.

Should enums be uppercase Python?

By convention, enumeration names begin with an uppercase letter and are singular. The enum module is used for creating enumerations in Python.


2 Answers

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.

like image 62
Ethan Furman Avatar answered Oct 09 '22 04:10

Ethan Furman


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.

like image 26
Cody Piersall Avatar answered Oct 09 '22 02:10

Cody Piersall