2016-07-14 26 views
1

私はPython tutorialを読んでいたと私は理解できませんでした。このラインに出くわした:Pythonの組み込み識別子とは何ですか?

標準例外名は組み込みの識別子( キーワードは予約されていない)です。

built-in identifiersとは何ですか? open()のような組み込み関数があることはわかっています。つまり、インポートする必要のない関数です。パイソン、識別子で

+0

あなたの定義を与える次のような出力に

>>> 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'] 

を与えます'' builtin'''の名前は識別子であり、キーワードではありません。 ''組み込み関数をインポートしよう。 dir(builtins)の識別子の場合:print(識別子) '' '。 *組み込み*例外のそれぞれの名前/識別子とその他の組み込み関数を出力する必要があります。 – wwii

答えて

1

は、特定のエンティティに与えられた名前であり、それは私が書くとき、例えば、クラス、変数、関数等である:

some_variable = 2 
try: 
    x = 6/(some_variable - 2) 
except ZeroDivisionError: 
    x = None 

some_variablex両方がそのI識別子であります定義した。ここでは3番目の識別子は例外で、ビルトインの識別子です(つまり、使用する前にインポートまたは定義する必要はありません)。

予約キーワードは、オブジェクトを特定するのではなく、Python言語自体を定義するのに役立ちます。これらは、などimportforwhiletryexceptifelseが含まれて...

1

識別子は、 '変数名' です。ビルトインとは、Pythonに付属しており、インポートする必要のないビルトインオブジェクトです。それらは、fooと5を関連付けるのと同じ方法で識別子に関連付けられています。foo = 5と言います。

キーワードはdefのような特殊トークンです。識別子はキーワードにすることはできません。キーワードは「予約済み」です。

openのような組み込みキーワードの場合、と同じスペルの識別子を使用できます。だからopen = lambda: Noneと言うことができ、以前は名前がopenに関連付けられているビルトインを上書きまたは「シャドーイング」しています。組み込み関数を読みやすくするために、組み込み関数をシャドウするのは一般的には良い考えではありません。

3

これはまさにあなたのことだと思いますが、関数ではなく「while」のようなコマンドではなく、Pythonに組み込まれています。例えば

機能が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'] 

ドキュメントのバックアップ:

https://docs.python.org/2/reference/expressions.html

5.2。 Atoms 原子は表現の最も基本的な要素です。最も単純な原子は、あなたが得ることができる原子名前です

https://docs.python.org/2/reference/lexical_analysis.html#identifiers

識別子(も名前とも呼ばれる)

0

として生じる識別子識別子またはリテラル

ですすべての組み込み関数は次のコマンドを使用しています。

dir(__builtins__) 

それはあなたがこれらの組み込みコマンドは次のように我々は何を行うことができます確認したい場合は、標準の例外は `の一部であるhelp("<name_of_the_builin_function>")

>>> help("zip") 
Help on class zip in module builtins: 

class zip(object) 
| zip(iter1 [,iter2 [...]]) --> zip object 
| 
| Return a zip object whose .__next__() method returns a tuple where 
| the i-th element comes from the i-th iterable argument. The .__next__() 
| method continues until the shortest iterable in the argument sequence 
| is exhausted and then it raises StopIteration. 
| 
| Methods defined here: 
| 
| __getattribute__(self, name, /) 
|  Return getattr(self, name). 
| 
| __iter__(self, /) 
|  Implement iter(self). 
| 
| __new__(*args, **kwargs) from builtins.type 
|  Create and return a new object. See help(type) for accurate signature. 
| 
| __next__(self, /) 
|  Implement next(self). 
| 
| __reduce__(...) 
|  Return state information for pickling. 
関連する問題