首页 > 技术文章 > python学习笔记(七)——内置函数

TaoR320 2020-03-07 23:56 原文

builtins.py模块,是python的内建模块,在运行时会自动导入该模块。在该模块中定义了很多我们常用的内置函数,比如print,input 等。

在 builtins.py 模块中给出如下注释的文档字符串

Built-in functions, exceptions, and other objects.
内置函数,异常和其他对象。

该模块会在python 程序运行时自动导入,因此我们在没有导入任何模块的情况下可以使用 input 输入,使用 print 输出。我们通过 dir() 可以查看当前程序(.py 文件)引用的变量、方法和定义的类型列表。

>>> dir()
['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sys']

其中在 builtins.py 中定义了很多的内置函数,我们可以通过 print(fun_name.__doc__) 打印出函数的文档字符串查看函数文档。

例如:

>>> print(print.__doc__)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

另外,也可以通过 help(函数名) 查看函数的相关信息。详细用法请参考目录中 help() 函数。

下面让我们通过一些简单

推荐阅读