Re: [Python-ideas] Naming convention for Enums
On Fri, Sep 16, 2016 at 03:54:18AM +1000, Chris Angelico wrote: > On Fri, Sep 16, 2016 at 3:03 AM, Steven D'Apranowrote: > > 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 = 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 ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Naming convention for Enums
On Fri, Sep 16, 2016 at 4:06 AM, Guido van Rossumwrote: >> 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.) Well, my view was largely colored [1] by the official docs. Your view is broadly the same as my ideal preference, just without the caveats and flexibility of accepting it the other way. So should the docs be updated to upper-case everything? With or without an actual PEP 8 change, that would send a strong message to the community. I'm sure I'm not the only person who looks at official examples as a style guide as well as a syntax demo. ChrisA [1] Sorry [2], that pun was just terrible [2] Actually I'm not sorry. But the pun was still terrible. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Naming convention for Enums
On Thu, Sep 15, 2016 at 10:54 AM, Chris Angelicowrote: > On Fri, Sep 16, 2016 at 3:03 AM, Steven D'Aprano 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) ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Naming convention for Enums
On Fri, Sep 16, 2016 at 3:03 AM, Steven D'Apranowrote: > 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 ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Naming convention for Enums
On 09/15/2016 10:03 AM, Steven D'Aprano 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? Besides being CONSTANTS enum members are also Singletons, and since I find CamelCase easier to read than UPPERCASE I may use that instead; otherwise I leave it all lowercase. -- ~Ethan~ ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Naming convention for Enums
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? -- Steve ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Naming convention for Enums
On 14 September 2016 at 12:51, Chris Angelicowrote: > 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. Agreed - it's not clear to me that a prescriptive rule has much benefit here. The OP said "I believe that we need to make a decision as no decision is somewhat worse and causes a lot of confusion" - personally, I don't agree, I think it's fine to leave this to the individual. And of course, no matter how many times we stress that any rules we add are "only recommendations", projects like pycodestyle will add checks (as they should) and then projects that mandate a clean run of a style checker will treat the rule as mandatory. So I'm basically -1 on a PEP 8 rule at this stage, but if we have to have one, I agree that it should say something along the lines of "depends on how you're using the enum". Paul PS FWIW, I tend to think of enums as named constants, and so would typically use uppercase, not lowercase. But I wouldn't like to impose that on everyone :-) ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Naming convention for Enums
On Wed, Sep 14, 2016 at 9:20 PM, Bar Harelwrote: > 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 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 ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/