0

A function created within a class works when called directly but it does not work with called within the same class. Gives me global name not defined.

I looked at other posts and then I checked my brackets, spacing, etc. but unable to see the issue.

In the below code this works:

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

But this doesn't work:

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

Main Code:

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())
Varun Yadav
  • 43
  • 2
  • 11
  • ...your indentation is off. you need to up the indent of your methods by one level (i.e. they need to be a the same level as `__init__`). – hiro protagonist Apr 02 '19 at 15:12
  • please fix the code in your question first so everyone is on the same page on which code we are talking about exactly. did you indent *all* your methods? – hiro protagonist Apr 02 '19 at 15:16
  • thank you about, didn't know about that indentation but now i am getting an AttributeError: srtqualifier instance has no attribute 'spartSABR' – Varun Yadav Apr 02 '19 at 15:19
  • ...now the methods are indented too far. you need to go one level down with the indentation. and `__init__` is off now... – hiro protagonist Apr 02 '19 at 15:19
  • 1
    In `mkaequalifier`, `spartSABR()` should be `self.spartSABR()` (and so forth for the rest of those methods.) – Patrick Haugh Apr 02 '19 at 15:21
  • @PatrickHaugh : same issue did this: def makequalifier(self): qualifier = string.join([self.currency,"|",self.spartSABR,"|",self.modelname(self.mdlname),"|",self.dttype(self.datatype),"|",self.lastpart(self.dataconvention)]) return qualifier – Varun Yadav Apr 02 '19 at 15:27
  • @hiroprotagonist: before the methods where at the same level as def __init__ now they start just below __ . Where should I place them? – Varun Yadav Apr 02 '19 at 15:27
  • It works now with both of your help. Did the indentation on init and : def makequalifier(self): qualifier = string.join([self.currency,"|",self.spartSABR(),"|",modelname(self.mdlname),"|",dttype(self.datatype),"|",lastpart(self.dataconvention)]) return qualifier – Varun Yadav Apr 02 '19 at 15:30

1 Answers1

0

@Patrick Haugh spotted your bug. See Difference between methods and functions, in Python compared to C++ for an explanation. The linked answer is helpful if you are coming to Python from another language.

ralex
  • 378
  • 1
  • 6