Python 规范命名变量
因为名字不能乱起,要是你一个想法,他一个想法,大家都对对方的名称不了解,这样也很难持续下去,因此这里说一下变量规范命名的规则和注意事项。
1、变量定义规则:
1,变量名只能是字母,数字或者下划线的任意组合
2,变量名的第一个字符不能是数字
3,关键字不能生成变量名
2、变量规范命名注意事项:
1,变量名不能过长
2,变量名词不达意思
3,变量名为中文,拼音
3、总体命名规则:
1,尽量单独使用小写字母‘l’,大写字母‘O’等容易混淆的字母。
2,模块命名尽量短小,使用全部小写的方式,可以使用下划线。
3,包命名尽量短小,使用全部小写的方式,不可以使用下划线。
4,类的命名使用CapWords的方式,模块内部使用的类采用_CapWords的方式。
5,异常命名使用CapWords+Error后缀的方式。
6,全局变量尽量只在模块内有效,类似C语言中的static。实现方法有两种,一是__all__机制;二是前缀一个下划线。
7,函数命名使用全部小写的方式,可以使用下划线。
8,常量命名使用全部大写的方式,可以使用下划线。
9,类的属性(方法和变量)命名使用全部小写的方式,可以使用下划线。
10,类的属性有3种作用域public、non-public和subclassAPI,可以理解成C++中的public、private、protected,non-public属性前,前缀一条下划线。
11,类的属性若与关键字名字冲突,后缀一下划线,尽量不要使用缩略等其他方式。
12,为避免与子类属性命名冲突,在类的一些属性前,前缀两条下划线。比如:类Foo中声明__a,访问时,只能通过Foo._Foo__a,避免歧义。如果子类也叫Foo,那就无能为力了。
13,类的方法第一个参数必须是self,而静态方法第一个参数必须是cls。
以上内容为大家介绍了Python规范命名变量,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python中的filter函数功能是什么?
python中的filter函数功能是什么?在python中,面对众多的数据,我们要过滤筛选出我们需要的数据。python中的filter函数就是起到了过滤筛选的作...详情>>
2023-11-10 20:37:27
pythontime模块是什么
pythontime模块是什么在python中使用时间,就免不了和time模块打交道,另外两个模块这个暂时先不做介绍。做time模块的使用上,我们可以用它来对...详情>>
2023-11-10 15:53:16
python是什么编程语言
python是什么编程语言1、说明是一种面向对象、解释型计算机程序设计语言,由GuidovanRossum于1989年底发明,第一个公开发行版发行于1991年。Pyt...详情>>
2023-11-10 15:21:05
python异常处理的两种技巧
python异常处理的两种技巧1、传递异常有时我们会在捕捉到一个异常后重新引发它(传递异常),实现起来很简单,使用不带参数的raise语句即可。deff...详情>>
2023-11-10 14:49:39热门推荐
python中的filter函数功能是什么?
沸python delattr函数如何使用?
热python中pdb模块怎么用?
热Python如何截图保存?
新python中缺少module怎么办?
python strftime和strptime的不同分析
python time.strptime的格式化
python中@contextmanager是什么?
python对象的三要素是什么
pythonGIL在Python多线程的应用
python如何对多个CSV文件进行读取
pythonif嵌套命令如何理解?
python对列表进行永久性或临时排序的方法
python生成器调用方法引发异常
技术干货






