**
标识符
**
python标识符及其命名规则
标识符是变量、函数、类、模块和其他对象的名称。
1、在 Python 里,标识符由字母、数字、下划线组成。
2、所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。
3、Python 中的标识符是区分大小写的。
4、以下划线开头的标识符是有特殊意义的。
以双下划线开头的 __foo 代表类的私有成员,不能直接从外部调用需通过类里
的其他方法调用。
以双下划线开头和结尾的 __foo__ 代表 Python 里特殊方法专用的标识,
如 __init__() 代表类的构造函数。
5、避免使用python预定义标识符名作为自定义标识符名。
例如,NotImplemented、Ellipsis、int、float、list、str、tuple等
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
保留关键字
关键字即预定义保留标识符。
关键字不能在程序中用作标识符,否则会产生编译错误。
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
python预定义标识符
Python 语言包含了许多预定义的内置类、异常、函数等,
如 float、input、print、ArithmeticError等,应避免使用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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
python命名规则
类型 命名规则 举例
模块名/包名 全小写字母,简单有意义如果需要可以使用下划线 math、sys
函数名 全小写字母,可以使用下划线增加可阅读性 foo(), my_func()
变量名 全小写字母,可以使用下划线增加可阅读性 age、my_var
类名 采用驼峰命名规则,多个单词组成名称每个单词的首字母大写 MyClass
常量名 全部大写字母,可以使用下划线增加可阅读性 LEFT、TAX_RATE
- 1
- 2
- 3
- 4
- 5
- 6