[Python-ideas] Naming convention for Enums

Guido van Rossum guido at python.org
Thu Sep 15 14:06:37 EDT 2016


On Thu, Sep 15, 2016 at 10:54 AM, Chris Angelico <rosuav at gmail.com> wrote:
> 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.

I disagree. The whole point of an enum is to define a new *kind* of
constant. Clearly RED and BLUE are new constants. (The uppercase
convention is not just for giving names to literals -- that's not what
"constant" means.)

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

Amen.

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

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list