2024-04-27 02:21:30 发布
网友
作为一个初学者,我理解的是Python标准库(PSL)提供了很多模块,这些模块提供了很多功能,但是如果我想使用这些模块,那么我必须导入模块,例如,sys,os等。这些模块是PSL模块,但是仍然需要导入。你知道吗
sys
os
现在,我想知道如果是这样的话,那么在不导入任何东西的情况下,我怎么能够使用像print、list、len等函数呢。?是不是他们的“支持是内置在翻译中的”?你知道吗
print
list
len
print函数来自builtins模块。 您可以找到它的文档here。你知道吗
builtins
下面是一个示例会话。 我首先检查print来自哪个模块,它存储在__module__属性中。 然后,导入builtins模块,检查其print函数是否与前缀less print相同。你知道吗
__module__
>>> print.__module__ 'builtins' >>> import builtins >>> builtins.print("hello") hello >>> print is builtins.print True
是的。它们是内置函数(或者在list的情况下是内置类)。如果希望对名称进行限定访问,可以显式导入the ^{} module(Py2)或the ^{} module(Py3),但默认情况下,只要尝试访问全局名称时在模块全局中找不到名称,就会搜索这些模块。但根据文件,它们通常不需要:
This module is not normally accessed explicitly by most applications, but can be useful in modules that provide objects with the same name as a built-in value, but in which the built-in of that name is also needed.
您应该读取built-in函数的页面
引用:
The Python interpreter has a number of functions and types built into it that are always available.
print
函数来自builtins
模块。 您可以找到它的文档here。你知道吗下面是一个示例会话。 我首先检查
print
来自哪个模块,它存储在__module__
属性中。 然后,导入builtins
模块,检查其print
函数是否与前缀lessprint
相同。你知道吗是的。它们是内置函数(或者在} module (Py2)或the ^{} module (Py3),但默认情况下,只要尝试访问全局名称时在模块全局中找不到名称,就会搜索这些模块。但根据文件,它们通常不需要:
list
的情况下是内置类)。如果希望对名称进行限定访问,可以显式导入the ^{您应该读取built-in函数的页面
引用:
相关问题 更多 >
编程相关推荐