python - builtins.True 语法错误

标签 python python-3.x boolean built-in

为什么下面的代码会产生语法错误?

>>> 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']
>>> builtins.True
  File "<stdin>", line 1
    builtins.True
            ^
SyntaxError: invalid syntax

我知道在 Python 3.X 中 TrueFalse 是保留字,但是调用 builtins.True 我只是调用了一个属性模块对象 builtins?

最佳答案

True 是保留关键字,这意味着您也不能将其用作属性名称。与名称一样,属性必须是有效的 Python 标识符。

您仍然可以使用 getattr() 将对象作为属性访问:

>>> import builtins
>>> getattr(builtins, 'True')
True

这可以在以下文档中找到:

  • Attribute references在表达式文档中:

    attributeref ::=  primary "." identifier
    
  • Identifiers and keywords在词法分析文档中:

    The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers.

    False [...] True

    强调我的。

关于python - builtins.True 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25528709/

相关文章:

python - 如何按行读取数据并返回数据帧

python - 为什么我的函数要修改输入变量?

c++ - VS2012 在 64 位目标中 vector <bool> 的性能不佳

MySQL Select from table 返回全部,加上带有条件的一列和另一个查询

python - Django - 数据库错误 : No such table

python - 如何在 Pandas 数据框中找到某个值最后一次出现的位置?

python - 如何在 Django 中以编程方式批准所有已安装的应用程序权限

python-3.x - python asyncio - 如何等待取消的屏蔽任务?

python-3.x - pandas to_numeric 无法将字符串值转换为整数

c++ - 为 bool 类型定义名称代替 "true"和 "false"