繁体   English   中英

Python-TypeError:未绑定的方法beamDeflection()必须以Beam实例作为第一个参数调用(取而代之的是获取列表实例)

[英]Python - TypeError: unbound method beamDeflection() must be called with beam instance as first argument (got list instance instead)

在Python中,我试图在一个类中运行一个函数并得到以下错误:

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

问题可能是因为您不是在beam的实例上而是在静态beam类本身上调用beamDeflection()

假设是问题所在,您可能会像这样重写getTotalDeflection方法:

def getTotalDeflection(self, x):
"""Calculate total deflection of beam due to multiple loads"""
    loadsList = self.Loads
    self.beamDeflection(loadsList, x)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM