一个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)