【问题标题】:TypeError: unbound method loadDisplacement() must be called with person instance as first argument (got float64 instance instead)TypeError: unbound method loadDisplacement() must be called with person instance as first argument (得到float64实例)
【发布时间】:2015-02-10 06:51:34
【问题描述】:

这是我目前写的代码: 我对 python 很陌生,所以我正在尝试使用最基本的方法来实现目标,因为我目前不知道如何提高效率等。

def simulateBeamRun(personlist, beam, times):
    times = np.linspace(0,35,500)
    templist = []
    maxdeflectionlist = []
    for t in times:
        for i in personlist: #for each person instance
            Tuple = personModel.person.loadDisplacement(t)
            if 0 < Tuple(1) < beamModel.beam.L:
                templist.append(Tuple)
            else:
                pass
    return templist

文件“beamSimulation.py”,第 40 行,在模拟BeamRun 中 元组 = personModel.person.loadDisplacement(t)

我得到的错误是:

TypeError: unbound method loadDisplacement() must be called with person instance as first argument (got float64 instance instead)

personlist 是一个列表列表,每个列表都包含给定“人”的到达时间、体重、步态、速度。这是为了给构造函数赋值。负载位移是 person 类中唯一的其他函数:

class person(object):
    """This class models the displacement of a person's load as they run at 
    'speed' in one dimension. It assumes that the load is always concentrated 
    in a single point and that the displacement of that point is less than or 
    equal to the displacement of the person's centre of mass. Also the 
    displacement of the load will always be a multiple of 'gait'.
    """
    def __init__(self, arrivalTime, weight, gait, speed):
        """This constructor function defines the person's weight, gait and 
        running speed as well as the time that they arrive at the position of 
        zero displacement.
        """

我该如何解决这个问题?

【问题讨论】:

    标签: list typeerror


    【解决方案1】:

    鉴于提供的代码有限,其中一些只是猜测,但它至少可以为您指明正确的方向:

    1. 如果您打算立即用times = ... 覆盖它,则无需传入times 参数。
    2. 您没有将maxdeflectionlist 用于任何事情,所以它并不是真正需要的(尽管您可能计划稍后...)。
    3. 在您的for i in ... 循环内,i 是您的迭代变量,应该从personlist 中依次获取每个值。从变量名称猜测,这些可能是您需要从中获取位移的person 实例,因此您收到错误的行可能应该是Tuple = i.loadDisplacement(t)。如果不是这种情况,考虑到您后来的 cmets,也许您需要从 i 中的数据实例化一个 person 对象 - 类似于 p = personModel.person(some, arguments, extracted, from, i),然后使用 Tuple = p.loadDisplacement(t)。像你一样调用loadDisplacement()更适合类方法或静态方法,而不是实例方法,这是你得到的错误信息背后的本质含义。它告诉您 personModel.person 不是 person 实例 - 它可能是一个类对象。
    4. else: pass 位有点毫无意义。

    【讨论】:

      猜你喜欢
      • 2019-10-30
      • 2018-01-20
      • 2021-11-23
      • 2012-05-15
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      相关资源
      最近更新 更多