作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 python 3 中,
>>> import keyword
>>> keyword.kwlist
和
>>> import builtins
>>> dir(builtins)
是两个不同的列表,但它们有一些共同的值,特别是
>>> set(dir(builtins)) & set(keyword.kwlist)
{'False', 'True', 'None'}
python中关键字和内置函数有什么区别?什么时候是 'False'、'None'、'True'
关键字以及它们是什么时候内置的? (如果这有什么不同的话)
最佳答案
关键字是解析器处理的核心语言结构。这些词是保留词,不能用作标识符:http://docs.python.org/reference/lexical_analysis.html#keywords
内置是常用的预加载函数、常量、类型和异常的列表:http://docs.python.org/library/functions.html
在 Python 3 中,重叠的词 False、None 和 True 是内置常量,不会被解析器赋值。这可以防止意外覆盖 True=10
。作为关键字,可以阻止此分配:
>>> True = 10
SyntaxError: assignment to keyword
其余的内置函数不 protected ,可以用 __builtins__.list = mylist
之类的东西重新分配。
关于 python 3 : What is the difference between keywords and builtins?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8204542/
我是一名优秀的程序员,十分优秀!