Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的内置标识符是什么?_Python_Python 2.7 - Fatal编程技术网

Python中的内置标识符是什么?

Python中的内置标识符是什么?,python,python-2.7,Python,Python 2.7,我在阅读时遇到了一行我不明白的话: 标准异常名称是未保留的内置标识符 关键词 什么是内置标识符?我知道有一些内置函数,如open,即我们不需要导入的函数。在Python中,标识符是给定给特定实体的名称,可以是类、变量、函数等。例如,当我编写: some_variable = 2 try: x = 6 / (some_variable - 2) except ZeroDivisionError: x = None 一些_变量和x都是我定义的标识符。这里的第三个标识符是ZeroDi

我在阅读时遇到了一行我不明白的话:

标准异常名称是未保留的内置标识符 关键词


什么是内置标识符?我知道有一些内置函数,如open,即我们不需要导入的函数。

在Python中,标识符是给定给特定实体的名称,可以是类、变量、函数等。例如,当我编写:

some_variable = 2
try:
    x = 6 / (some_variable - 2)
except ZeroDivisionError:
    x = None
一些_变量和x都是我定义的标识符。这里的第三个标识符是ZeroDivisionError异常,它是一个内置标识符,也就是说,在使用它之前,不必导入或定义它


这与保留关键字形成对比,保留关键字不标识对象,而是帮助定义Python语言本身。这些包括导入、for、while、try、except、if、else等。在Python中,标识符是给定给特定实体的名称,可以是类、变量、函数等。例如,当我编写:

some_variable = 2
try:
    x = 6 / (some_variable - 2)
except ZeroDivisionError:
    x = None
一些_变量和x都是我定义的标识符。这里的第三个标识符是ZeroDivisionError异常,它是一个内置标识符,也就是说,在使用它之前,不必导入或定义它


这与保留关键字形成对比,保留关键字不标识对象,而是帮助定义Python语言本身。这些包括导入、for、while、try、except、if、else等。

标识符是“变量名”。内置是Python附带的内置对象,不需要导入。它们与标识符关联的方式与我们通过foo=5将5与foo关联的方式相同

关键字是像def这样的特殊标记。标识符不能是关键字;关键字为“保留”


但是,对于像open这样的内置关键字,可以使用拼写相同的标识符。因此,您可以说open=lambda:None,并且您已经覆盖或“隐藏”了以前与open名称关联的内置函数。一般来说,隐藏内置代码不是一个好主意,因为它会提高可读性。

标识符是“变量名”。内置是Python附带的内置对象,不需要导入。它们与标识符关联的方式与我们通过foo=5将5与foo关联的方式相同

关键字是像def这样的特殊标记。标识符不能是关键字;关键字为“保留”


但是,对于像open这样的内置关键字,可以使用拼写相同的标识符。因此,您可以说open=lambda:None,并且您已经覆盖或“隐藏”了以前与open名称关联的内置函数。一般来说,对内置进行阴影处理不是一个好主意,因为它会提高可读性。

这正是您所认为的,它不是函数,也不是像while那样的命令,而是Python内置的。e、 g

函数类似于open,关键字类似于while,标识符类似于True或IOError

其中更多:

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 
'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis',
 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 
'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 
'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 
'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 
'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 
'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 
'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 
'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 
'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', 
'_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 
'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 
'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 
'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 
'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 
'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 
'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 
'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 
'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 
'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 
'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 
'unicode', 'vars', 'xrange', 'zip']
文件备份:

5.2。原子 原子是表达式中最基本的元素。最简单的原子是标识符或文字

作为原子出现的标识符是名称

标识符也称为名称


这正是您所认为的,一个东西的名称,它不是一个函数,也不是像while那样的命令,它内置于Python中。e、 g

函数类似于open,关键字类似于while,标识符类似于True或IOError

其中更多:

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 
'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis',
 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 
'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 
'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 
'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 
'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 
'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 
'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 
'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 
'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', 
'_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 
'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 
'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 
'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 
'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 
'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 
'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 
'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 
'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 
'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 
'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 
'unicode', 'vars', 'xrange', 'zip']
文件备份:

5.2。原子 原子是表达式中最基本的元素。最简单的原子是标识符或文字

作为原子出现的标识符是名称

标识符也称为名称


您可以使用以下命令获取所有内置项

dir(__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', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', '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']
然后,如果您想检查我们可以如何处理这些内置项,下面将为您提供定义帮助


您可以使用以下命令获取所有内置项

dir(__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', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', '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']
然后,如果您想检查我们可以如何处理这些内置项,下面将为您提供定义帮助


标准的异常是内置代码的一部分,它们的名称只是标识符,而不是关键字。尝试导入内置;对于目录内置的标识符:printidentifier。它应该打印每个内置异常和其他内置异常的名称/标识。@wwii这些都不是python知道的所有标识符,例如cls缺失、\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu新、\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu init\u?如果您为名称cls分配一个字符串,您是否会隐藏Python本身使用的标识符。。。是的,cls似乎不是一个内置标识符,但uuu init和uu new应该是。我只是想知道一些特殊的方法名。Th
标准异常是内置的一部分,它们的名称只是标识符,而不是关键字。尝试导入内置;对于目录内置的标识符:printidentifier。它应该打印每个内置异常和其他内置异常的名称/标识。@wwii这些都不是python知道的所有标识符,例如cls缺失、\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu新、\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu init\u?如果您为名称cls分配一个字符串,您是否会隐藏Python本身使用的标识符。。。是的,cls似乎不是一个内置标识符,但uuu init_uuuuuu和uuu new_uuuuuuuu应该是。我只是想知道一些特殊的方法名。