說說 Python 關鍵字與內建函式

程式設計中所面臨的一個挑戰就是給變數指定合適的名稱,但不能是關鍵字或是內建函式。
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()。