python - 如何将 "args=(...)"添加到我自己函数的参数中?

标签 python numpy scipy arguments numerical-methods

在 scipy 中有一个函数 quad():

quad(integrand, 0, 1, args=(a,b))

我有自己的函数来使用高斯legendgre求积分,当我将不同的函数与参数相结合时,我有点厌倦了添加它们。所以问题是:

有没有办法将参数 args=(...) 添加到我自己函数的参数中?

最佳答案

当然,

def my_function(value1, value2, value3, args=(0,0)):
    print(value1, value2, value3, args)

# call with only 3 values
# it will use args=(0,0) as default
my_function(1,2,3)

# call with 3 values and args
my_function(4,5,6, args=(7,8))

输出:

1 2 3 (0, 0)
4 5 6 (7, 8)

关于python - 如何将 "args=(...)"添加到我自己函数的参数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50019969/

相关文章:

python - 使用 scipy.curve_fit 进行全局拟合

Python Peewee重置主键

python - 在 Python 中为日期添加时间

python - 将 numpy 数组保存到不带括号和空格的文件中

python - 使用 Python 共享内存的段错误

python - Pandas 改进

python - 如何理解 scipy.linalg.lu_factor 的主元矩阵?

python - 仅绘制正加权边 NetworkX

python - 从奇数 Dd HH :MM:SS. mm 格式中提取时间数据

python - 从列表构造 numpy 数组