在类中创建的函数在直接调用时有效,但在同一个类中调用时不起作用。给了我未定义的全局名称。我查看了其他帖子,然后检查了我的括号、间距等,但无法看到问题所在。在下面的代码中,这是有效的: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贡献1783条经验 获得超4个赞
@Patrick Haugh 发现了您的错误。有关解释,请参阅Python 与 C++ 中方法和函数之间的差异。如果您是从另一种语言开始使用 Python,则链接的答案会很有帮助。
添加回答
举报
0/150
提交
取消