Python3中有True、False、None关键字还是内置关键字?

Python3中有True、False、None关键字还是内置关键字?,python,python-3.x,keyword,nonetype,Python,Python 3.x,Keyword,Nonetype,因此,在Python2中,这一点很清楚。但是在Python3中,我有点困惑 >>> import builtins >>> dir(builtins) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessE

因此,在Python2中,这一点很清楚。但是在Python3中,我有点困惑

>>> import builtins
>>> dir(builtins)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
>>> import keyword
>>> dir(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
内置模块和关键字模块中都存在True、False和None。那我该怎么治疗他们呢?作为内置类或关键字?

它们是保留字和内置值。从:

True
False
None
是保留字。(2.6部分实施了对
None
的限制。)

这意味着您不能将它们用作名称,而是为它们指定不同的值。这样可以防止意外屏蔽内置的单例值:

>>> True = False
  File "<stdin>", line 1
SyntaxError: can't assign to keyword
>True=False
文件“”,第1行
SyntaxError:无法分配给关键字
另见:

我还是忘了回答None/True/False是文字还是关键字。我的回答是他们都是。它们是关键字,因为解析器就是这样识别它们的。它们是文字,因为这是它们在表达式中的角色,因为它们代表常量值

通过将
True
False
None
分类为关键字,Python编译器实际上可以优化它们的使用,因为您不能(直接)重新绑定这些名称,Python可以将它们作为常量而不是全局变量进行查找,这会更快


在Python3.4之前,仍然存在编译器会对这些对象进行全局查找的情况,请参阅。从Python3.4开始,Python解析器已经被扩展,以生成一个新的AST节点
nameContent
,以确保它们在任何地方都被视为常量

但是如果
None
True
False
现在都是Python关键字,为什么要将它们保留在内置模块中?@ostrokach:名称可能是关键字,但它们仍然需要引用实际对象。使用该名称时,仍希望将该名称解析为对象。为什么要破坏任何使用
内置.False
内置.True
绕过本地阴影的代码?