[Python-ideas] Naming convention for Enums

Chris Angelico rosuav at gmail.com
Wed Sep 14 07:51:32 EDT 2016


On Wed, Sep 14, 2016 at 9:20 PM, Bar Harel <bzvi7919 at gmail.com> wrote:
> Enums in Python are a unique creature - it's a class, the members aren't
> injected into module level, it can have methods, but it's attributes are
> somewhat consts (although they can be modified via __new__).
>
> Although I'm in favor of lower-case instead of upper-case, I believe that we
> need to make a decision as no decision is somewhat worse and causes a lot of
> confusion.

The trouble is that enums are a bit of a chimera - part constant, part
class attribute, part class instance, part integer (often). You can
treat them as "constant ints with nice reprs", or you can treat them
as "representations of set membership and nothing more", and all
manner of other uses.

I would accept a PEP 8 ruling that they be in lower-case *only* if
it's qualified with a blanket permission to name something the same as
its C counterpart when one exists.

>>> socket.AF_INET
<AddressFamily.AF_INET: 2>

There is absolutely no value in lower-casing something like this.
(Even leaving aside backward compatibility; imagine this situation
with a brand-new third party module that provides Python bindings to a
C library.) If they're going to be constants that match the C
constants, they should have names that match the C names.

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.

ChrisA


More information about the Python-ideas mailing list