如何查看Python内置函数列表
# 如何查看Python内置函数列表?
在Python开发中,内置函数是系统预先定义好的“开箱即用”工具,掌握它们的列表和功能能大幅提升编码效率。本文将详细介绍5种查看Python内置函数的方法,涵盖交互式命令、官方文档及IDE工具等场景,帮助你快速定位所需函数。
## 一、使用`dir(__builtins__)`:快速获取内置函数名称列表
### 核心原理
`__builtins__`是Python内置命名空间的模块对象,存储了所有内置函数、类和异常。通过`dir()`函数可以列出该对象包含的所有成员名称。
### 操作步骤
1. **打开Python交互式环境**
在终端输入`python`或`python3`进入:
```bash
$ python3
Python 3.9.7 (default, Sep 4 2022, 08:14:34)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
```
2. **执行`dir(__builtins__)`**
```python
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip', '__import__']
```
- **输出说明**:结果包含内置函数、类、异常等名称,其中以小写字母开头的通常是函数(如`abs`、`len`),大写开头的多为类或异常(如`list`、`TypeError`)。
3. **过滤出仅函数名称**
```python
>>> [name for name in dir(__builtins__) if callable(getattr(__builtins__,name))]
['abs', 'all', 'any', 'ascii', ..., 'sorted', 'sum', 'zip']
```
- **原理**:通过`callable()`判断对象是否可调用(函数是可调用的,类和异常不可调用)。
## 二、使用`help()`:查看内置函数详细文档
### 基础用法
在交互式环境中直接调用`help()`进入帮助系统,查询内置函数的文档说明:
```python
>>> help()
Welcome to Python 3.9's help utility!
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
help> builtins
```
### 查看单个函数文档
无需进入帮助系统,直接查询某个函数的用法:
```python
>>> help(abs)
Help on built-in function abs in module builtins:
abs(x, /)
Return the absolute value of the argument.
```
- **关键信息**:包含函数签名(参数类型、是否必需)、功能描述、返回值说明。
## 三、查阅Python官方文档:最权威的参考资料
### 访问方式
打开浏览器,进入Python官方文档的**内置函数**页面:
[https://docs.python.org/3/library/functions.html](https://docs.python.org/3/library/functions.html)
### 页面结构
1. **按字母排序的函数列表**
每个函数名对应一个详细章节,如`abs()`、`all()`、`dict()`等。
2. **详细说明**
以`len()`为例:
```
len(obj)
Return the number of items in a container.
```
- 描述:返回容器(如列表、字符串、字典)中的元素个数。
- 参数:`obj`为任意可迭代对象。
- 返回值:整数。
## 四、利用IDE自动补全功能:编码时快速检索
在PyCharm、VS Code等集成开发环境中,输入`builtins.`或函数名前缀,会触发自动补全提示,显示所有内置函数:
### 操作技巧
- **过滤搜索**:输入关键词(如`str`)可快速定位相关函数(如`str.lower()`、`str.split()`)。
- **查看文档**:选中函数后按`F1`(PyCharm)或`Ctrl+Q`(VS Code)可直接查看文档。
## 五、通过代码动态筛选:编程场景下的灵活获取
### 1. 区分函数与类/异常
```python
import inspect
# 获取所有内置函数
builtin_functions = [
name for name, obj in inspect.getmembers(__builtins__, inspect.isfunction)
]
# 获取所有内置类
builtin_classes = [
name for name, obj in inspect.getmembers(__builtins__, inspect.isclass)
]
```
### 2. 打印函数功能概览
```python
for func_name in builtin_functions:
func = getattr(__builtins__, func_name)
print(f"{func_name}: {func.__doc__.splitlines()[0]}")
```
**输出示例**:
```
abs: Return the absolute value of the argument.
all: Return True if bool(x) is True for all values x in the iterable.
any: Return True if any element of the iterable is true.
```
## 六、常见问题解答
### Q1:内置函数和标准库函数有什么区别?
- **内置函数**:直接隶属于Python解释器,无需导入即可使用(如`len()`、`type()`)。
- **标准库函数**:属于Python标准库模块(如`os.path.join()`、`json.dumps()`),需导入模块后使用。
### Q2:如何判断一个对象是否为内置函数?
```python
>>> from builtins import abs
>>> isinstance(abs, type(lambda:0)) # 内置函数是function类型
True
>>> isinstance(list, type(lambda:0)) # 内置类不是函数
False
```
## 总结:按需选择最佳查看方式
| 场景 | 推荐方法 | 优势 |
|---------------------|-------------------------|---------------------------------------|
| 快速获取名称列表 | `dir(__builtins__)` | 无需网络,一行命令直达 |
| 查阅详细用法 | `help()` 或官方文档 | 权威说明+示例,适合深入学习 |
| 编码时实时查询 | IDE自动补全 | 无缝集成开发流程,效率最高 |
| 动态筛选或批量处理 | 代码过滤(`inspect`) | 适合脚本自动化处理场景 |