在類(lèi)中創(chuàng)建的函數(shù)在直接調(diào)用時(shí)有效,但在同一個(gè)類(lèi)中調(diào)用時(shí)不起作用。給了我未定義的全局名稱(chēng)。我查看了其他帖子,然后檢查了我的括號(hào)、間距等,但無(wú)法看到問(wèn)題所在。在下面的代碼中,這是有效的:test1 = srtqualifier(mdlname,datatype,dataconvention,t1)print (test1.spartSABR())但這不起作用:print(test1.makequalifier()) ##NameError: global name 'spartSABR' is not defined主要代碼:import stringdatatype = "Swaption SABR Normal Volatility"dataconvention = "USD-SIFMA Municipal Swap Index-1W-SWAPTION-PHYSICAL-SEMI-BOND"mdlname = "IR_SABR"t1 = 'should-send-back-none'#datatype = raw_input("Please input Data Type: ")#dataconvention = raw_input("Please input Data Convention: ")#modelname = raw_input("Please provide model name: ")class srtqualifier:def __init__(self,mdlname,datatype, dataconvention,t1): self.mdlname = mdlname self.datatype = datatype self.dataconvention = dataconvention self.t1 = t1 self.tempholder = self.dataconvention.split("-") self.currency = self.tempholder[0] def spartSABR(self): secondpartsp = self.tempholder[1].split(" ") secondpartsp.append(self.tempholder[2]) separator = "_" secondpart = separator.join(secondpartsp) secondpart = secondpart.upper() return secondpart def modelname(self): mdname = {'IR_SABR':'SABR','FX_LN':'FXLN','IR_LGM':'LGM','IR_LMM':'LMM','IR_SABR_FX_LN_GC':'SABR_FX_LN_GC','IR_SABR_GC':'SABR_GC','INFLATION_SABR':'SABR_IF'} return mdname.get(self.mdlname)
1 回答

慕娘9325324
TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
@Patrick Haugh 發(fā)現(xiàn)了您的錯(cuò)誤。有關(guān)解釋?zhuān)?qǐng)參閱Python 與 C++ 中方法和函數(shù)之間的差異。如果您是從另一種語(yǔ)言開(kāi)始使用 Python,則鏈接的答案會(huì)很有幫助。
添加回答
舉報(bào)
0/150
提交
取消