[Python]编码规范性(五)——命名(包和模块、类、函数)

命名规范推荐

  • Packages:lower_with_under
  • Modules:lower_with_under
  • Class:CapWords(UpperCamelCase)
  • Exceptions:CapWords
  • Functions:lower_with_under()
  • Global/Class Constants:CAPS_WITH_UNDER
  • Global/Class Variables:lower_with_under
  • Instance Variables:lower_with_under
  • Method Names:lower_with_under()
  • Function/Method Parameters:lower_with_under
  • Local Variables:lower_with_under

包和模块:

(必须遵守)(规则):

1、包(Packages)、模块(Module)名使用意义完整的英文描述,采用小写加下划线(lower_with_under)的风格命名;

说明:模块应该用小写加下划线的方式(如:lower_with_under.py)命名;

尽管很多现存的模块使用类似于CapWords.py这样的命名,但现在已经不鼓励这样做,因为如果模块名碰巧和类名一致,这会让人产生困扰;

类:

(必须遵守)(规则):

类(Class)名使用意义完整的英文描述,采用大写字母开头的单词(CapWords)风格命名;

说明:类沿用面向对象语言最常用的CapWords风格命名——UpperCamelCase(大驼峰命名法)

示例:

Class SampleClass(object):
pass

函数:

(必须遵守)(规则):

函数(Function)、方法(Method)、函数参数(Function Parameters)使用意义完整的英文描述,采用小写加下划线(lower_with_under)的风格命名;

说明:函数、方法采用小写加下划线的风格命名,与类名做区分;

函数采用小写加下划线的风格命名,与一般变量的命名风格保持一致;

模块内部使用的函数用单下划线(_)开头,表示函数是protected的(使用from module import *时不会包含);

def sample_public_function(sample_parameter):
pass
def sample_internal_function(sample_parameter):
pass
class SampleClass(object):
def sample_member_method(self, sample_parameter):
pass
posted @   MoKin_Li  阅读(1593)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示