Python:使用缩略语命名

Ber*_*rnd 1 python coding-style

在Python代码中,在命名类,方法和变量时,处理众所周知的首字母缩略词的规范方法是什么?

例如,考虑一个处理RSS提要的类.那会是这样的:

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

class ClassRSSTwo:
    def MethodRSS(self):
        self.variable_RSS = True
Run Code Online (Sandbox Code Playgroud)

或这个:

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

class ClassRssTwo:
    def MethodRss(self):
        self.variable_rss = True
Run Code Online (Sandbox Code Playgroud)

更重要的是,保留首字母缩写词或PEP 008的建议?

编辑:从答案中,我得出结论,这将是要走的路:

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

class ClassRSSTwo:
    def method_rss(self):
        self.variable_rss = True
Run Code Online (Sandbox Code Playgroud)

iCo*_*dez 9

好吧,事实证明PEP 8已经涵盖这个主题:

注意:在CapWords中使用缩写时,请将缩写的所有字母大写.因此HTTPServerErrorHttpServerError.

换句话说,包含首字母缩略词的名称的Python约定是:

  1. 在类名中保留大写的缩写(通常是使用CapWords的Python的唯一部分).

  2. 在其他任何地方,将它们设为小写以符合其他命名约定.

以下是ipaddress模块的演示:

>>> 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>
>>>
Run Code Online (Sandbox Code Playgroud)