在Python,我想在一个类中运行的功能,并出现以下错误:的Python - 类型错误:不受约束的方法beamDeflection()必须与梁例如被称为第一个参数(有替代列表实例)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:/Users/X/Downloads/beamModel.py", line 58, in getTotalDeflection
beam.beamDeflection(loadsList, x)
TypeError: unbound method beamDeflection() must be called with beam instance as first argument (got list instance instead)
代码:beamModel.py:
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 beamDeflection(self, Load, x):
"""Calculates defeclection at point x due to single load"""
Force, distance = Load
L = self.L
E = self.E
I = self.I
b = L - distance
i = (Force*b)/(6*L*E*I)
j = ((L/b)*(x-distance)**3 - x**3 + (L**2 - b**2)*x)
k = (L**2 - x**2 - b**2)
if x > distance:
return i*j
else:
return i*k
def getTotalDeflection(self, x):
"""Calculate total deflection of beam due to multiple loads"""
loadsList = self.Loads
beam.beamDeflection(loadsList, x)
现在我需要运行通过函数beamDeflection元组(这是工作)的列表,总结的结果给我的总挠度值,应由函数getTotalDeflection返回。
编辑:
我改变了getTotalDeflection功能:
def getTotalDeflection(self, x):
"""Calculate total deflection of beam due to multiple loads
"""
loadsList = self.Loads
self.beamDeflection(loadsList, x)
这是现在给我的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:/Users/X/Downloads/beamModel.py", line 58, in getTotalDeflection
self.beamDeflection(loadsList, x)
File "C:/Users/X/Downloads/beamModel.py", line 39, in beamDeflection
Force, distance = Load
ValueError: need more than one value to unpack
你能按照你的方法更清楚哪些/功能光束类的方法,哪些是模块级。你的缩进表明'beamDeflection'不是一个梁的实例方法,而'self'参数表明它是。 – schwobaseggl
@schwobaseggl完全正确,您显示的代码缺少重要元素。看起来缩进是错误的,并且很难知道getTotalDeflection是否属于beam对象。 – Ire
事实上,你学习Python的一个很好的起点可能是看一下样式指南https://www.python.org/dev/peps/pep-0008/ –