Skip to main content
 首页 » 编程设计

python之Python中子类的函数返回错误

2025年05月04日17jyk

我正在编写一些代码,其中包含一个通用父类(super class)和更具体的子类,这些子类的属性略有不同。我已经设置了一个子类并在其中定义了一个函数,但是当我调用该函数时(如 subclass.function(args)),我收到一条错误消息,指出我包含了错误数量的参数:

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) 

请您参考如下方法:

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