7

In Python code, what is the canonical way of dealing with well-known acronyms when naming classes, methods and variables?

Consider, for example, a class dealing with RSS feeds. Would that rather be this:

class RSSClassOne:
    def RSSMethod(self):
        self.RSS_variable = True

class ClassRSSTwo:
    def MethodRSS(self):
        self.variable_RSS = True

or this:

class RssClassOne:
    def rssMethod(self):
        self.rss_variable = True

class ClassRssTwo:
    def MethodRss(self):
        self.variable_rss = True

I.e. what is more important, keeping the acronym capitalization or the recommendations of PEP 008?

Edit: from the answers, I conclude that this would be the way to go:

class RSSClassOne:
    def rss_method(self):
        self.rss_variable = True

class ClassRSSTwo:
    def method_rss(self):
        self.variable_rss = True
Bernd
  • 3,390
  • 2
  • 23
  • 31
  • If you would like to work on open-source projects, you should definitely work with pep8. Other than that, keep classes Capilatized and method names with snake_case... – oz123 Mar 02 '14 at 21:14
  • Naming conventions become non-trivial when you apply tools like pylint to the development process. – Carl Aug 18 '16 at 08:37

3 Answers3

23

Well, it turns out that PEP 8 already has this topic covered here:

Note: When using abbreviations in CapWords, capitalize all the letters of the abbreviation. Thus HTTPServerError is better than HttpServerError.

In other words, the Python convention for names containing acronyms is:

  1. Keep acronyms uppercase in class names (usually, the only part of Python that uses CapWords).

  2. Everywhere else, make them lowercase in order to comply with the other naming conventions.

Below is a demonstration with the ipaddress module:

>>> import ipaddress  # IP is lowercase because this is a module
>>> ipaddress.IPv4Address  # IP is uppercase because this is a class
<class 'ipaddress.IPv4Address'>
>>> ipaddress.ip_network  # IP is lowercase because this is a function
<function ip_network at 0x0242C468>
>>>
  • Good example. What about method names? PEP8 says it should be words_with_underscores. I would normally write my_rss_method() – rafgoncalves Mar 02 '14 at 22:48
  • I would recommend adding this observation to the answer. Both examples provided on the question have faulty method names. – rafgoncalves Mar 02 '14 at 23:13
  • Actually, I just found a PEP 8 excerpt that explains exactly what to do in this situation. So, I'll go with that instead. –  Mar 03 '14 at 01:48
  • True about the faulty method names, I have been looking too long at Twisted. Learned something new, thanks. – Bernd Mar 03 '14 at 06:48
1

I don't think that there is anything wrong with the first one at all. As an acronym represents a series of words and in Python class names you do CamelCase at the start of each word, it is perfectly OK to capitalise each letter (word) in the acronym.

The first code example, therefore conforms to the style guide for Python code probably more than the second. In short, the first one because PEP8 conformation is extremely important. :)

Bear in mind also that there are sometimes (albeit very rarely) instances in which these rules can be bent slightly. This could be considered one of them.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
0

I would say that following the recommendation in PEP 8 is the first thing to do related to coding style.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
Javier
  • 1,027
  • 14
  • 23