python - 有没有办法遍历模块的所有功能?

标签 python math module

我有一个数字列表,我想(例如)第一项和第二项执行导入的 math 模块中的每个函数。

import math
list = [1, 2, 3]
#do stuff that will print (for example) 2+1, 2-1, 2/1, etc.

最佳答案

这是一个简单的方法。您需要指定如果函数不需要两个参数会发生什么。

for name in dir(math):
    item = getattr(math, name)
    if callable(item):
        item(list[0], list[1])

关于python - 有没有办法遍历模块的所有功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36670844/

相关文章:

json - Node.JS:如何抓取 json 页面以获取特定数据

python - 无法使用 easy_install 安装 Python 模块

使用 random.sample() 时出现 Python 内存错误

python - 在 python 中测试非导出方法

python - 如何使用 python-docx 更改标题样式?

python - 如何在 Python Tensorflow-Keras 中 pickle weakref?

algorithm - 双正方形 : counting numbers which are sums of two perfect squares

algorithm - 通过区间拟合函数

java - 在计算之前判断指数是否高于长阈值

php - 创建自定义 joomla 验证码