Python - TypeError:float对象不可调用错误

时间:2015-12-18 23:45:45

标签: python

到目前为止已获得以下代码:

class beam(object):

    def __init__(self, E, I, L):
         self.E = E  
         self.I = I  
         self.L = L  
         self.Loads = [(0.0, 0.0)] #[(Force, distance along beam)]

    def getTotalDeflection(self, x):
        """Calculate total deflection of beam due to multiple loads"""
        return sum(self.beamDeflection(loadall, x) for loadall in self.Loads)

    def getSlope(self, x):
        """Calculate gradient at a point x on beam due to deflection
        """
        import scipy.misc
        return scipy.misc.derivative(self.getTotalDeflection, x)

对于函数getSlope(),我需要通过找到相对于x的偏转导数来计算斜率。但是,我收到以下错误:

  

文件“C:/Users/X/Downloads/beamModel.py”,第12行,在       class beam(object):文件“C:/Users/X/Downloads/beamModel.py”,第67行,在梁中       print b.getSlope(1.0)getSlope中的文件“C:/Users/X/Downloads/beamModel.py”,第62行       return scipy.misc.derivative(self.getTotalDeflection,x)文件“C:\ Users \ X \ Anaconda2 \ lib \ site-packages \ scipy \ misc \ common.py”,行   258,衍生品       val + = weights [k] * func(x0 +(k-ho)* dx,* args)TypeError:'float'对象不可调用

1 个答案:

答案 0 :(得分:1)

发生这种情况的唯一方法是将float传递给derivative()代替函数:

>>> scipy.misc.derivative(1.0, 1.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/scipy/misc/common.py", line 195, in derivative
    val += weights[k]*func(x0+(k-ho)*dx,*args)
TypeError: 'float' object is not callable

您是否完全确定您的代码是否已发布?您的代码很可能实际上是调用 self.getTotalDeflection,因此将其返回值(浮动)传递给derivative(),例如你的代码可能是:

return scipy.misc.derivative(self.getTotalDeflection(x), x)

或类似的,当它真的应该像你发布的那样,即

return scipy.misc.derivative(self.getTotalDeflection, x)