这是我到目前为止编写的代码:我对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 中 Tuple = 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.
"""
我该如何解决这个问题?
鉴于所提供的代码有限,其中一些只是猜测,但它可能会为您指明正确的方向,至少:
- 如果您只是要立即用
times = ...
覆盖它,则无需传入times
参数。 - 您没有将
maxdeflectionlist
用于任何事情,因此它并不是真正需要的(尽管您可能打算稍后使用...... - 在
for i in ...
循环中,i
是迭代变量,应依次从personlist
获取每个值。从变量名称猜测,这些可能是您需要从中获取位移的person
实例,因此您收到错误的行可能应该是Tuple = i.loadDisplacement(t)
。如果不是这种情况,鉴于您后面的评论,也许您需要从i
中的数据实例化一个person
对象 - 类似于p = personModel.person(some, arguments, extracted, from, i)
,然后跟着Tuple = p.loadDisplacement(t)
.按原样调用loadDisplacement()
更适合类方法或静态方法,而不是实例方法,这是您获得错误消息背后的基本含义。它告诉你personModel.person
不是一个person
实例 - 它可能是一个类对象。 else: pass
位有点毫无意义。