python变量通过变量名访问_Python:通过变量名访问实例的方法?

一个Python初学者。抱歉,如果这个问题是愚蠢的。我只是试图通过从字典中读入的“变量化”对象/实例名称来访问实例类的方法(addComponentToIC)。在

我创建的字典如下所示:qaDict = {} # a place to store the DSIDs/names for later use

jd = r.findDSIDs('My QA Team', 'External') # make the call to get unique IDs

for a in jd:

(a['lastName']).lower() + '_' + (a['firstName']).lower() #name the object using "last_name_firstName"

if a['dsid'] not in blacklistedDSIDs:

globals()[qaperson] = Ic(a['dsid'], a['lastName'], a['firstName']) ## create the Ic object

qaDict[a['dsid']] = qaperson ## add person to list of DSIDs

然后我要获取这些ID,并获取它们作为验证程序的所有组件:

^{pr2}$

我得到一个:AttributeError: 'unicode' object has no attribute 'addComponentToIC'

我也尝试过:for key, value in qaDict.items():

jd = r.findComponents(key)

value.addComponentToIC(jd['id'], jd['name'])

这也引发了同样的错误。因此,这似乎与变量名“value”有关,而不是实际解释为实例名。它应该看起来像:employees_name.addComponentToIC(jd['id'], jd['name'])

--其中“employees_name”是先前创建的类的一个实例,但它不起作用。我知道这里有些愚蠢的事情我不明白;—)任何帮助都将不胜感激!在

我的班级是这样的:class Ic(object):

'This is a class for QA folks'

empCount = 0

@classmethod

def displayEmployeeCount(class_obj):

print ("The total employee count is %d" % Ic.empCount)

def __string__(self):

return str(self)

def __init__(self, dsid, fname, lname):

self.dsid = dsid

self.fname = fname

self.lname = lname

self.name = fname + ' ' + lname

Ic.empCount += 1

self.ticketCount = 0

self.componentCount = 0

self.componentDict = {}

self.componentName = ''

#Ic.instances.add(self)

def addComponentToIC(self, componentID, componentName):

self.componentName = componentName

self.componentID = componentID

self.componentDict[componentID] = componentName

@classmethod

def get_instances(cls):

return list(Ic.instances)

def addTicketCount(self, count):

self.ticketCount += count

def addComponentCount(self, count):

self.componentCount += count

def displayComponentNumber(self):

print (self.name, " has ", len(self.componentDict), " number of components.")

def displayEmployee(self):

print ("Name: ", self.name, "DSID: ", self.dsid, "Count: ", self.ticketCount, ", Component Count: ", len(self.componentDict))

def displayComponentDict(self):

print ("This employee is verifier for the following components: ", self.componentDict)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值