1
我使用vars()
“即时创建”一些对象变量。这似乎不是最干净的操作。但是,除此之外,我还可以如何生成对象的属性名称,而无需手动定义它们(在这种情况下,字典相对较短,但这只是一些测试数据)。我知道here描述的这种做法带来的问题。使用var()创建字典键的属性名称
class DerivedProf(Prof):
def __init__(self,profiel, TAG, code_DB):
Prof.__init__(self, profiel, TAG)
self.CountCodes(self.attr, code_DB)
def CountCodes(self, attr, code_DB):
count = 0
for key, value in code_DB.iteritems():
if value[0].lower() == 'true':
for i,p in enumerate(attr):
if int(attr[i].code2) == value[1]:
count += 1
else:
continue
vars(self)[key] = count
code_DB = {'code_72': ['true',72],
'code_74': ['true',74],
'code_76': ['true',76],
'code_88': ['true',88]}
这正是我一直在寻找的!谢了哥们。 – LarsVegas 2012-08-10 09:05:24