子类在Python中返回错误的功能

时间:2014-02-28 22:18:27

标签: python python-2.7

我正在编写一些代码,这些代码由一般超类和更具体的子类组成,这些子类具有稍微不同的属性。我已经设置了一个子类并在其中定义了一个函数,但是当我调用该函数时(如在,subclass.function(args)中)我得到一个错误,说我包含了错误的参数数量:< / p>

TypeError                                 Traceback (most recent call last)
<ipython-input-5-45d26bca20e5> in <module>()
     80 
     81 example = CR0098('test',10,10)
---> 82 example.Curve(34)

TypeError: Curve() takes exactly 1 argument (2 given)

我似乎无法解决这个问题。代码如下所示:

import math
class Spectra(object):
    def __init__(self, name, PGA=0.3, Damping=5):
        self.name = name
        self.PGA = PGA
        self.Damping = Damping

class CR0098(Spectra):
   def __init__(self, name, vaRatio, ADV, PGA=0.3, Damping=5):
        Spectra.__init__(self,name,PGA,Damping)
        self.vaRatio = vaRatio
        self.ADV = ADV


   def Curve(freq):
    if self.Damping == 0.5: #Factors from NUREG 98 Table 3 based on damping percentage
        AperAprime = 5.1
        VperVprime = 3.84
        DperDprime = 3.04
    elif self.Damping == 1:
        AperAprime = 4.38
        VperVprime = 3.38
        DperDprime = 2.73
    elif self.Damping == 2:
        AperAprime = 3.66
        VperVprime = 2.92
        DperDprime = 2.42
    elif self.Damping == 3:
        AperAprime = 3.24
        VperVprime = 2.64
        DperDprime = 2.24
    elif self.Damping == 5:
        AperAprime = 2.71
        VperVprime = 2.3
        DperDprime = 2.01
    elif self.Damping == 7:
        AperAprime = 2.36
        VperVprime = 2.08
        DperDprime = 1.85
    elif self.Damping == 10:
        AperAprime = 1.99
        VperVprime = 1.84
        DperDprime = 1.69
    else:
        pass
    factor = 386.4 #Factor converts g's to in per sec^2
    vPrime = self.vaRatio * self.PGA #V' calculation
    dPrime = self.ADV *(vPrime**2)/(self.PGA*factor)
    A = self.PGA*AperAprime*factor #Peak Spectral Acceleration
    V = vPrime * VperVprime #Peak Spectral Velocity
    D = dPrime * DperDprime #Peak spectral displacement

    #Control Points
    sF1 = 0.1 #Freqency for control point 1
    sA1 = D/((2*math.pi*(0.1))**2) #Point 1 with freqency of 0.1 hz
    sF2 = (V/D)/(2*math.pi) #Frequency for point 2 in hz
    sA2 = D/((2*math.pi*(sF2))**2) #Acceleration for Point 2
    sF3 = (A/V)/(2*math.pi) #frequency for point 3 in hz
    sA3 = V/(2*math.pi*sF3) # Acceleration for point 3 
    sF4 = 8 #Frequency for control point 4
    sA4 = sA3 #Acceleration for control point 4, same as point 3
    sF5 = 33 #Frequency for control point 5, rigid
    sA5 = self.PGA #Acceleration for control point 5

    aSlope = math.log10(sA5/sA4)/math.log10(sF5/sF4) #Acceleration slope factor

    dispA = D * ((2*math.pi * freq)**2)/factor #Spectral Acceleration based on spectral displacement
    velA =  V * (2*math.pi * freq)/factor #Spectral Acceleration based on spectral velocity
    AFactor = math.log10(sA4)+math.log10(freq/sF4)*aSlope #Special acceleration factor

    if freq < sF4: #calculates the acceleration based on which frequency range
        accelA1 = A/factor
    else:
        accelA1 = 10**AFactor
    accelA = max(accelA1, sA5) #verifies that the acceleration is above the rigid acceleration

    SpectralAcceleration = min(dispA,velA,accelA)
    return SpectralAcceleration

example = CR0098('test',10,10)
example.Curve(34)

3 个答案:

答案 0 :(得分:2)

您忘记了方法定义中的self参数。

答案 1 :(得分:2)

您忘记了self定义中的Curve参数:

def Curve(self, freq):

第一个隐式参数始终是当前实例的引用。有关这个非常常见的主题的更多解释,请参阅What is the purpose of self?

答案 2 :(得分:2)

您需要使用self作为第一个参数来定义方法:

def Curve(self, freq):

每次在其他参数前面调用实例方法时,都会隐式传递此参数。如上所述,self被传递给freq参数,没有任何东西可以接受你的实际的 freq参数。


来自self上的documentation

  

从方法中引用对象的成员没有简介:方法函数使用表示对象的显式第一个参数声明,该参数由调用隐式提供。