[Python-ideas] Naming convention for Enums

Steven D'Aprano steve at pearwood.info
Thu Sep 15 14:32:07 EDT 2016


On Fri, Sep 16, 2016 at 03:54:18AM +1000, Chris Angelico 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.

Yes, but when would you write something like this?

Color.red = <new Enumeration value>


That's not to be confused with the case:

text_colour = Color.RED
# later...
text_colour = Color.BLUE

where *text_colour* is clearly used as a variable, not a constant. But 
the enumerations themselves are still constant.



-- 
Steve


More information about the Python-ideas mailing list