说说 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()。
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容