【问题标题】:Python - TypeError: float object is not callable errorPython - TypeError: float object is not callable 错误
【发布时间】:2016-03-25 18:04:04
【问题描述】:

目前已经得到如下代码:

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 行,在 类梁(对象):文件“C:/Users/X/Downloads/beamModel.py”,第 67 行,在梁中 在 getSlope 中打印 b.getSlope(1.0) 文件“C:/Users/X/Downloads/beamModel.py”,第 62 行 返回 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' object is not callable

【问题讨论】:

  • 已编辑以修复复制和粘贴的错误。

标签: python


【解决方案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)

【讨论】:

    猜你喜欢
    • 2022-08-23
    • 2014-11-16
    • 2022-11-20
    • 2021-08-08
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2021-10-22
    相关资源
    最近更新 更多