只是想知道为什么
import sys
exit(0)
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in ?
exit(0)
TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud)
但
from sys import exit
exit(0)
Run Code Online (Sandbox Code Playgroud)
工作良好?
Python仅将所选名称导入命名空间.
你应该得到同等的第一个解决方
sys.exit(0)
Run Code Online (Sandbox Code Playgroud)
因为import sys只将sys关键字导入当前命名空间.
有关在Python中使用的所有不同方法,请参阅http://effbot.org/zone/import-confusion.htmimport.
导入系统
这将导入sys模块并将其绑定到命名空间中的名称"sys"."exit",sys模块的其他成员不会直接进入命名空间,但可以这样访问:
sys.exit(0)
Run Code Online (Sandbox Code Playgroud)
来自sys import exit
这会将sys模块的特定成员导入您的命名空间.具体来说,这会将名称"exit"绑定到sys.exit函数.
exit(0)
Run Code Online (Sandbox Code Playgroud)
要查看命名空间中的内容,请使用该dir函数.
>>> import sys
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>>
>>> from sys import exit
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'exit', 'sys']
Run Code Online (Sandbox Code Playgroud)
您甚至可以看到sys模块本身的内容:
>>> dir(sys)
['__displayhook__', '__doc__', '__egginsert', '__excepthook__', '__name__', '__package__', '__plen', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
632 次 |
| 最近记录: |