How is __builtin__ available at runtime?

Why does the first statement return NameError, but maxis available

>>> __builtin__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__builtin__' is not defined
>>> max
<built-in function max>
>>> import __builtin__
>>> __builtin__.max
<built-in function max>
+5
source share
3 answers

The builtins namespace associated with code execution is actually found by looking for a name __builtins__in its global namespace; it must be a dictionary or module (in the latter case, a dictionary of modules is used). By default, when a module __main__ __builtins__has a built-in module __builtin__(note: no 's); when in any other module __builtins__is an alias for the dictionary of the module itself __builtin__. __builtins__can be installed in a user dictionary to create a weak form of limited execution.

, __builtins__ ( )

>>> __builtins__.max
<built-in function max>

, , __builtin__ ( , __builtins__).

+4

__builtin__ - / , , . . __builtin__ __builtin__, , .

. python.

+4

import __builtin__, . , __builtin__ .

+2

All Articles