-
内建名称空间
-
全局名称空间
1
2
3
4
|
>>>
abs
(
-
1
)
1
>>>
max
(
1
,
3
)
3
|
1
2
3
4
5
|
>>>
abs
<built
-
in
function
abs
>
>>>
>>>
max
<built
-
in
function
max
>
|
1
2
|
>>>
dir
()
[
'__builtins__'
,
'__doc__'
,
'__name__'
,
'__package__'
]
|
1
2
|
>>>
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'
,
'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'
]
|
1
2
|
>>> __builtins__
<module
'__builtin__'
(built
-
in
)>
|
1
2
3
4
5
|
>>>
import
__builtin__
>>>
import
__builtins__
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
ImportError: No module named __builtins__
|
-
在Python中并没有__builtins__这个模块,只有__builtin__模块,__builtins__模块只是在启动Python解释器时,解释器为我们自动创建的一个到__builtin__模块的引用
1
2
|
>>>
print
__name__
__main__
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
>>>
import
__builtin__
>>> __builtin__
<module
'__builtin__'
(built
-
in
)>
>>> __builtins__
<module
'__builtin__'
(built
-
in
)>
>>> __builtin__.__name__
'__builtin__'
>>> __builtins__.__name__
'__builtin__'
>>> __builtins__
=
=
__builtin__
True
>>> __builtins__
is
__builtin__
True
>>>
id
(__builtins__)
140295127423752
>>>
id
(__builtin__)
140295127423752
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
>>>
def
func():
...
print
'test'
...
>>> func
<function func at
0x7f99012bdc08
>
>>> funcs
<function func at
0x7f99012bdc08
>
>>> func.__name__
'func'
>>> funcs.__name__
'func'
>>> funcs
=
=
func
True
>>> funcs
is
func
True
>>>
id
(funcs)
140295126375432
>>>
id
(func)
140295126375432
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#!/usr/bin/env python
import
__builtin__
print
'Module name:'
, __name__
print
'*==test __builtin__ and __builtins__==*'
print
'__builtin__ == __builtins__'
, __builtin__
=
=
__builtins__
print
'__builtin__ is __builtins__'
, __builtin__
is
__builtins__
print
'id(__builtin__)'
,
id
(__builtin__)
print
'id(__builtins__)'
,
id
(__builtins__)
print
'='
*
50
print
'*==test __builtin__.__dict__ and __builtins__==*'
print
'__builtin__.__dict__ == __builtins__'
, __builtin__.__dict__
=
=
__builtins__
print
'__builtin__ is __builtins__'
, __builtin__.__dict__
is
__builtins__
print
'id(__builtin__)'
,
id
(__builtin__.__dict__)
print
'id(__builtins__)'
,
id
(__builtins__)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>>
import
test
Module name: test
*
=
=
test __builtin__
and
__builtins__
=
=
*
__builtin__
=
=
__builtins__
False
__builtin__
is
__builtins__
False
id
(__builtin__)
140592847690504
id
(__builtins__)
140592847925608
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
*
=
=
test __builtin__.__dict__
and
__builtins__
=
=
*
__builtin__.__dict__
=
=
__builtins__
True
__builtin__
is
__builtins__
True
id
(__builtin__)
140592847925608
id
(__builtins__)
140592847925608
|
-
如果是在主模块__main__中,__builtins__直接引用__builtin__模块,此时模块名__builtins__与模块名__builtin__指向的都是同一个模块,即<builtin>内建模块(这里要注意变量名和对象本身的区别)
-
如果不是在主模块中,那么__builtins__只是引用了__builtin__.__dict__