gpt4 book ai didi

python - 什么是 python 中的默认模块(当我们运行 Python 时导入它们,例如 "print")

转载 作者:行者123 更新时间:2023-11-28 19:31:35 25 4
gpt4 key购买 nike

启动 Python 编译器时自动导入的 Python 中有哪些默认模块?

例如,print、加法和其他数学函数。
它们必须在 Python 的某处定义

我猜想我们可以通过查找来提取此信息(例如,如果我们知道此信息,我们可以从中获取 print 函数的来源)。

最佳答案

“模块”在 Python 中有特殊的含义。 “打印”和“添加”都不是模块。

+和其他“数学函数”是运算符,而for , in , if等是关键字,不是函数,因此不会出现在 __builtins__ 中。 .

如果您真的想知道启动 Python 时加载了哪些模块,请查看 sys.modules.keys() :

$ python -i
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.modules.keys()
['copy_reg', 'sre_compile', 'locale', '_sre', 'functools', 'encodings', 'site',
'__builtin__', 'sysconfig', 'operator', '__main__', 'types', 'encodings.encodings',
'abc', '_weakrefset', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll',
'ntpath', '_codecs', 'nt', '_warnings', 'genericpath', 'stat', 'zipimport',
'encodings.__builtin__', 'warnings', 'UserDict', 'encodings.cp1252', 'sys',
'codecs', 'os.path', '_functools', '_locale', 'signal', 'traceback', 'linecache',
'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']

关于python - 什么是 python 中的默认模块(当我们运行 Python 时导入它们,例如 "print"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7643809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com