在类中返回未定义的全局名称的函数

2024-09-27 21:27:41 发布

您现在位置:Python中文网/ 问答频道 /正文

在类中创建的函数在被直接调用时可以工作,但在同一类中被调用时不能工作。未定义全局名称

我看了其他文章,然后我检查了我的括号,间距等,但无法看到问题

在下面的代码中,这起作用:

test1 = srtqualifier(mdlname,datatype,dataconvention,t1)
print (test1.spartSABR())

但这行不通:

print(test1.makequalifier()) ##NameError: global name 'spartSABR' is not defined

主代码:

import string

datatype = "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)

    def dttype(self):
        dtype = {'Swaption SABR Normal Volatility':'ATM_NORMAL_VOLATILITY','Swaption SABR Rho':'RHO','Swaption SABR Nu':'NU',
                'Swaption SABR Beta':'BETA','Swaption SABR Par Yield Correlation Adjustment':'PAR_YIELD_CORRELATION',
                'Swaption SABR Par Yield Convexity Adjustment':'PAR_YIELD_CONVEXITY','OU MEAN REVERSION':'OU_MEAN_REVERSION',
                 'Libor Market Model Displaced Diffusion Coefficient':'DISPLACED_DIFFUSION_COEFFICIENT',
                 'Indices Spread CapFloorlet Correlation':'INDICES_SPREAD_CAPFLOORLET_CORRELATION',
                 'Indices Spread CapFloorlet Correlation by Strike Minus ATM':'INDICES_SPREAD_CAPFLOORLET_CORRELATION_BY_STRIKE_MINUS_ATM',
                 'Indices Spread CapFloorlet Correlation by Strike':'INDICES_SPREAD_CAPFLOORLET_CORRELATION_BY_STRIKE',
                 'Quanto Swaption GC Correlation':'QUANTO_SWAPTION_GC_CORRELATION',
                 'Inflation SABR Beta Ratio':'INFLATION_SABR_BETA_RATIO','Inflation SABR IR Corr':'INFLATION_SABR_IR_CORRELATION',
                 'Inflation SABR Nu Ratio':'INFLATION_SABR_NU_RATIO','Inflation SABR Nu ZC':'INFALTION_SABR_NU_ZC',
                 'Inflation SABR Rho Ratio':'INFLATION_SABR_RHO_RATIO','Inflation SABR Rho ZC':'INFLATION_SABR_RHO_ZC',
                 'Inflation SABR Vol ATM Ratio':'INFLATION_SABR_VOL_ATM_RATIO','Inflation SABR Vol ATM ZC':'INFLATION_SABR_VOL_ATM_ZC',
                 'CapFloorlet Linear Exponential Parameter beta':'BETA','Indices Spread CapFloorlet Correlation Term Structure':'INDICIED_SPREAD_CAPFLOORLET_CORRELATION_TERM_STRUCTURE',
                 'OU Correlation':'OU_CORRELATION'}
        return dtype.get(self.datatype)

    def lastpart(self):
        td = self.t1.split("-")
        if any("PHYSICAL" in s for s in td):
            return 'PHYSICALLY_CLEARED'
        elif any("SEMI_ACT365F" in s for s in td):
            return 'SEMI_ACT365F'
        elif any("ANNUAL_BOND" in s for s in td):
            return 'ANNUAL_BOND'
        elif any("CAPLOORLET" in s for s in td):
            return 'CAPLOORLET'

    def makequalifier(self):
        qualifier = string.join([self.currency,"|",spartSABR(),"|",modelname(self.mdlname),"|",dttype(self.datatype),"|",lastpart(self.dataconvention)])
        return qualifier



test1 = srtqualifier(mdlname,datatype,dataconvention,t1)
print (test1.spartSABR())
print(test1.makequalifier())

Tags: inselfreturnirt1datatypecorrelationatm

热门问题