说说 Python 关键字与内置函数
2019-02-04 本文已影响24人
deniro
编程中所面临的一个挑战就是给变量指定合适的名称,但不能是关键字或是内置函数。
1 关键字
这些关键字都有特殊的含义, 如果我们将它们用作变量名, 就会引发错误:
| 关键字 | 关键字 | 关键字 | 关键字 | 关键字 |
|---|---|---|---|---|
| False | class | finally | is | return |
| None | continue | for | lambda | try |
| True | def | from | nonlocal | while |
| and | del | global | not | with |
| as | elif | if | or | yield |
| assert | else | import | pass | |
| break | except | in | raise |
2 内置函数
如果我们将这些内置函数名用作变量名, 那么就会覆盖这些函数,从而改变它们的行为:
| 内置函数 | 内置函数 | 内置函数 | 内置函数 | 内置函数 |
|---|---|---|---|---|
| abs() | divmod() | input() | open() | staticmethod() |
| all() | enumerate() | int() | ord() | str() |
| any() | eval() | isinstance() | pow() | sum() |
| basestring() | execfile() | issubclass() | print() | super() |
| bin() | file() | iter() | property() | tuple() |
| bool() | filter() | len() | range() | type() |
| bytearray() | float() | list() | raw_input() | unichr() |
| callable() | format() | locals() | reduce() | unicode() |
| chr() | frozenset() | long() | reload() | vars() |
| classmethod() | getattr() | map() | repr() | xrange() |
| cmp() | globals() | max() | reversed() | zip() 和 Zip() |
| compile() | hasattr() | memoryview() | round() | __import__() |
| complex() | hash() | min() | set() | apply() |
| delattr() | help() | next() | setattr() | buffer() |
| dict() | hex() | object() | slice() | coerce() |
| dir() | id() | oct() | sorted() | intern() |
注意:
- Python 2.x 中,print 是关键字而不是函数 。
- Python 3.x 中,没有内置函数 unicode()。