2014-02-28 27 views
1

我正在寫一些由一般超類和更具體的子類組成的代碼,它們將具有稍微變化的屬性。我已經設置了一個子類並在其中定義了一個函數,但是當我調用函數時(如in,subclass.function(args)),我得到一個錯誤,說我包含錯誤的參數個數:在Python中的子類的函數返回錯誤

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) 
+0

你忘了在定義行中添加'self' :) – Roberto

回答

2

您忘記了方法定義中的self參數。

+0

完美。再一次,我的無知無處不在。謝謝,完全修復它。 – Tsnorthern

2

你忘了self參數在Curve定義:

def Curve(self, freq): 

第一個隱含參數始終是當前實例的引用。請參閱What is the purpose of self?以獲取關於這個非常常見的主題的更多解釋。

2

您需要與self定義方法的第一個參數:

def Curve(self, freq): 

這個參數每次調用一個實例方法時隱式傳遞,在其他參數前面。正如所寫的,自我正在傳遞給freq的論點,沒有什麼可以接受你的實際freq的論點。


documentationself

有從它的方法引用對象的成員沒有速記:該方法函數被聲明與代表對象的顯式的第一個參數,它是由隱式地提供電話。