我正在尝试为字典中的变量分配一个值,但无法这样做。
我尝试过:
self.currency = inflationcurrency[self.currency]
self.currency = inflationcurrency.get([self.currency])
在顶部,我尝试将self.currency声明为
self.currency = ''.join(self.tempholder[0])
这是代码:
import string
datatype = "Inflation SABR Vol ATM ZC"
dataconvention = "UK-RPI-ZERO-COUPON-SWAP-RATE"
mdlname = "INFLATION_SABR"
t1 = 'should-send-back-none'
class srtqualifier:
def __init__(self,mdlname,datatype, dataconvention,t1):
self.mdlname = mdlname
self.datatype = datatype
self.dataconvention = dataconvention
self.t1 = t1 ##TO BE REMOVED, ONLY USE FOR TESTING##
self.tempholder = self.dataconvention.split("-")
self.currency = self.tempholder[0]
def curr(self):
if self.mdlname == "INFLATION_SABR":
inflationcurrency = {'UK':'GBP','FR':'EUR','EU':'EUR','US':'USD'}
self.currency = inflationcurrency.get(self.currency)
else:
return self.currency
def makequalifier(self):
qualifier = string.join([self.currency,"|",self.spartSABR(),"|",self.modelname(),"|",self.dttype(),"|",self.lastpart()])
return qualifier
test1 = srtqualifier(mdlname,datatype,dataconvention,t1)
print(test1.makequalifier())
上面的错误是预期的字符串,但从self.currency接收到Nonetype
当我做下面的事情时,我什么也没得到。
print (test1.currency())
答案 0 :(得分:0)
您同时使用货币作为变量和方法,请重命名其中之一。
---编辑---
您没有返回curr方法。
def curr(self):
if self.mdlname == "INFLATION_SABR":
inflationcurrency = {'UK':'GBP','FR':'EUR','EU':'EUR','US':'USD'}
self.currency = inflationcurrency.get(self.currency)
return self.currency
这应该有效。
答案 1 :(得分:0)
您是否正确访问它? inflationcurrency
没有名称为GBP
的密钥。
(Pdb) self.currency
'GBP'
Pdb) inflationcurrency
{'EU': 'EUR', 'FR': 'EUR', 'US': 'USD', 'UK': 'GBP'}
反转键值以使其起作用?
inflationcurrency = {'EUR': ['EUR', 'FR'], 'USD': 'US', 'GBP': 'UK'}