python——标识符及其命名规则

标识符

标识符是变量、函数、类、模块和其他对象的名称。

①在 Python 里,标识符由字母、数字、下划线组成。

    在 Python 中,所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。

②Python 中的标识符是区分大小写的。

③以下划线开头的标识符是有特殊意义的。

 以双下划线开头的 __foo 代表类的私有成员,__foo(self)代表类的私有方法,不能直接从外部调用,需通过类里的其他方法调用。

   以双下划线开头和结尾的 __foo__ 代表 Python 里特殊方法专用的标识,如 __init__() 代表类的构造函数。

④避免使用python预定义标识符名作为自定义标识符名。例如,NotImplemented、Ellipsis、int、float、list、str、tuple等

保留关键字

关键字即预定义保留标识符。

关键字不能在程序中用作标识符,否则会产生编译错误。

False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not  

python预定义标识符

Python 语言包含了许多预定义的内置类、异常、函数等,如 float、input、print、ArithmeticError等,应避免使用Python 预定义标识符名作为自定义标识符名。使用Python 内置函数 dir(__builtins__) 可以查看所有的内置异常名和函数名。

Python内置函数
abs() all() any() basestring() bin()
bool() bytearray() callable() chr() classmethod()
cmp() compile()  complex() delattr() dict()
dir() divmod() enumerate()  eval() execfile()
file() filter() float()  format() frozenset()
getattr() globals() hasattr() hash() help()
hex() id() input() int()  isinstance()
issubclass() iter() len() list() locals()
long() map()  max() memoryview() min()
next()  object() oct()  open()  ord()
pow() print() property() range() raw_input()
reduce() reload() repr() reversed() zip()
round()  set() setattr() slice() sorted()
staticmethod() str() sum() super() tuple()
type() unichr() unicode()  vars() xrange()
Zip() __import__() apply() buffer() coerce()
intern      

python命名规则

类型命名规则举例
模块名/包名 全小写字母,简单有意义,如果需要可以使用下划线 math、sys
函数名 全小写字母,可以使用下划线增加可阅读性 foo(), my_func()
变量名 全小写字母,可以使用下划线增加可阅读性 age、my_var
类名 采用PascalCase命名规则,由多个单词组成名称,其每个单词的首字母大写 MyClass
常量名 全部大写字母,可以使用下划线增加可阅读性 LEFT、TAX_RATE
原文地址:https://www.cnblogs.com/Bluebells/p/14321930.html