[Python-ideas] Naming convention for Enums

Chris Angelico rosuav at gmail.com
Thu Sep 15 13:54:18 EDT 2016


On Fri, Sep 16, 2016 at 3:03 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Wed, Sep 14, 2016 at 09:51:32PM +1000, Chris Angelico wrote:
>
>> Perhaps the advice needs to be along the lines of: Decide what the
>> purpose of the enum is, and follow a naming convention accordingly.
>> Uppercase if you're basically making constants; lowercase if you're
>> not; etcetera.
>
> I can't think of any situation where Enums would *not* be treated as
> constants. Can you give an example?

Sometimes they function as integer constants (esp IntEnum), and
sometimes more as just arbitrary values. See the examples in the docs
[1] for Color and Mood, where the exact value is immaterial, just as
long as Color.red is not Color.blue. Even though they happen to have
integer values, they're not intended to be used as actual integers.
For those cases, it's not as clear that they ought to be Color.RED and
Color.BLUE - it's equally acceptable to have them in lowercase.

But if one convention or the other had to be picked for all enums, I
would go with all-caps, since they're most often going to be
representing constant integers, per Guido's post. I don't know of
_any_ real-world cases where they're not integers, though there are
some examples in the docs.

ChrisA

[1] https://docs.python.org/3/library/enum.html


More information about the Python-ideas mailing list