Python问题。TypeError:\uuuuu call\uuuuuu()接受2个位置参数,但给出了3个

2024-05-07 23:59:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我很高兴任何人都能提供帮助!我正在为一个基本的傅立叶分析程序编写代码,为了计算我出于效率考虑而实现的系数集,我想将函数相乘,这样它就可以提供一个可以进行数值积分的单一函数。我得到了一些帮助,现在有了一个“可操作”的课程,应该可以帮助我做到这一点。以下是“可操作”类的代码,高于我的程序代码:


class operable:
    def __init__(self, f):
        self.f = f
    def __call__(self, x):
        return self.f(x)

def op_to_function_op(op):
    def function_op(self, operand):
        def f(x):
            return op(self(x), operand(x))
        return operable(f)
    return function_op

for name, op in [(name, getattr(operator, name)) for name in dir(operator) if "__" in name]:
    try:
        op(1,2)
    except TypeError:
        pass
    else:
        setattr(operable, name, op_to_function_op(op)) 

现在我调用一个可操作的函数,从第6行这里的函数调用

def findFourierCoefficients(function, T, kmax):
    coefficientsA = [] #will have size kmax
    coefficientsB = []
    for i in range(0, kmax):
        #find the coefficient a_k
        integrandA = integrandForAK(function, i) #HERE IS THE FUNCTION CALL
        integrationA = simpson(integrandA, 0, T, nValue)
        coefficientA = (2/T) * integrationA
        coefficientsA.append(coefficientA) 
    return coefficientsA

我在网上查看了一下,发现我需要为我的函数提供第三个参数,我在下面使用参数“self”完成了这项工作

@operable
def integrandForAK(self, fun, k):
    def specialCosineFunction(x):
        return np.cos(k * omega * x)
    functionToIntegrate = fun * specialCosineFunction
    #functionToIntegrate = getFunctionProduct(fun, specialCosineFunction)
    return functionToIntegrate

我仍然得到这个错误,当三个位置参数被给出时,它只需要两个位置参数——但是我在函数中做了另一个参数,所以我不知道为什么它不能识别它。再次感谢您提供的任何帮助,我对这一级别的编码还相当陌生,尤其是python对我来说是更新的。祝你一切顺利


Tags: 函数nameinselffor参数returndef