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

时间:2019-04-02 15:10:57

标签: python string class

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

我查看了其他帖子,然后检查了方括号,间距等,但看不到该问题。

在下面的代码中有效:

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())

1 个答案:

答案 0 :(得分:0)

@Patrick Haugh发现了您的错误。有关说明,请参见Difference between methods and functions, in Python compared to C++。如果您是从另一种语言开始使用Python,则链接的答案会很有帮助。