Python命名规则

原文来自这里: Naming Conventions

总的规则

  命名是给人看的,所以应该突出它的使用情况,而不是实现情况。也就是说,命名应该以问题为导向。

命名风格

  b  单个小写字母

  B  单个大写字母

  lowercase  小写情况

  lower_case_with_underscores 带下划线的小写情况

  UPPERCASE  大写情况

  UPPER_CASE_WITH_UNDERSCORES  带下划线的大写情况

  CapitalizedWords  驼峰式

  mixedCase  混合情况,和驼峰式不同,首字母小写

  Capitalized_Words_With_Underscores  带下划线的驼峰是结构

  _single_leading_underscore  以单下划线开头的变量,在from M import * 的时候不会被导入

  single_trailing_underscore_  以单下划线结尾的变量,用来避免python中的关键字,如class_是避免class

  __double_leading_underscore  以双下划线开头的变量,涉及到python中的名称改正

  __double_leading_and_trailing_underscore__  以双下划线开始和结尾的变量,涉及到python中“魔术”对象和属性,比如 __init__,__import__等

命名规则

避免使用的名字

小写字母‘l’ 大写字母‘O’,大写字母‘I’ 为了避免引起混乱,比如有的和数字1比较像,有的和数字0比较像。

ascii兼容性

必须和ascii兼容

包和模块命名规则

包:package,模块:module。 包是一个含有__init__的文件夹,模块是一个.py结尾的文件。

模块应该使用全部小写的名字,当名字长的时候可以使用下划线。包也是用小写字母,但是并不鼓励使用下划线。

类名称

类名应该使用驼峰式结构。

类变量名称

PEP 484类变量使用短小的驼峰式结构,推荐添加后缀_co和_contra。

异常变量

异常属于类,所以类当中使用的名称适合异常,但是如果异常属于错误,通常再后面加Error。

全局变量

和函数命名方法相同

函数和变量的名称

函数应该使用小写名称,并且用下划线隔开。变量命名和函数相同。有的时候使用混合结构即首字母小写的驼峰式结构,为了兼容一些代码。

函数和方法的参数

方法是一种放在类中的函数。实例方法第一个参数为self,类方法第一个参数为cls。如果参数和关键字相同,后面加一个下划线作为后缀。,其它情况和变量命名相同。

方法和实例变量

实例变量是直接定义在类中的变量,不在任何方法中定义。

使用函数的规则:小写字母并且用下划线分开。当方法和实例变量不是公开的,前面加一个前缀_。

常量

全部大写,多的话用下划线隔开。

继承的情况

 。。。未翻译


公共和内部的接口

使用__all__来包含那些公共的接口,同样,加前缀_,也表明这是一个内部接口。


总结一下,python当中除了类使用驼峰结构外,大部分变量,函数的命名都采用小写字母然后用下划线隔开的形式。










  • 1
    点赞
  • 0
    评论
  • 6
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

Machine Translation is the author’s comprehensive view of machine translation (MT) from the perspective of a participant in its history and development. The text considers MT as a fundamental part of Artificial Intelligence and the ultimate test-bed for all computational linguistics, covering historical and contemporary systems in Europe, the US and Japan. The author describes and contrasts a range of approaches to MT’s challenges and problems, and shows the evolution of conflicting approaches to MT towards some kind of skeptical consensus on future progress. The volume includes historic papers, updated with commentaries detailing their significance both at the time of their writing and now. The book concludes with a discussion of the most recent developments in the field and prospects for the future, which have been much changed by the arrival of the World Wide Web. Anyone interested in the progress of science and technology, particularly computer scientists and students, will find this a fascinating exploration of MT technology. Yorick Wilks is a Professor of Computer Science at the University of Sheffield, where he directs the Institute for Language, Speech and Hearing. He received his M.A. and Ph.D. (1968) from Pembroke College, Cambridge. He has also taught or researched at Stanford, Edinburgh, Geneva, Essex and New Mexico State Universities. His interests are artificial intelligence and the computer processing of language, knowledge and belief. He is a Fellow of the European and American Societies for Artificial Intelligence, a Fellow of the EPSRC College of Computing and a member of the UK Computing Research Council. Wilks was awarded the Antonio Zampolli prize by the European Language Resources Association in 2008. This prize is given to individuals whose work lies within the areas of Language Resources and Language Technology Evaluation with acknowledged contributions to their advancements. He was also the recipient of an ACL Life Achievement Award at the 46th Annual Meeting of the Association for Computational Linguistics in 2008.
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值