我已经定义了一些类:
class CustomParameter():
def __init__(self, strFriendlyAttribName, strSystemAttribName):
self.FriendlyAttribName = strFriendlyAttribName
self.SystemAttribName = strSystemAttribName
class PartMaster():
AttribNameList = ["Part Number", "Name", "Standard Part", "Part Type", "ControlledBy", "PIN", "Design Responsibility"]
def __init__(self):
self._UUID = None
self.PartNumber = CustomParameter("Part Number", "V_ID"),
self.Name = CustomParameter("Name", "V_name"),
self.StandardPart = CustomParameter("Standard Part", "V508_isStandardPart"),
self.PartType = CustomParameter("Part Type", "V511_PartType"),
self.ControlledBy = CustomParameter("ControlledBy", "V511_ControlledBy"),
self.PIN = CustomParameter("PIN", "BOECACPinItemNumber"),
self.DesignResponsibility = CustomParameter("Design Responsibility", "BOECACDesignRpnse")
class Part():
def __init__(self, PartNumber):
self.PartNumber = PartNumber
#This instance wraps
self.PartMaster = PartMaster() #create new instance
test = Part("ABC")
我希望test.PartMaster.PIN
是CustomParameter的一个实例,但它是一个元组tuple: (<__main__.CustomParameter instance at 0x0000000002D724C8>,)
为什么会这样,我怎么能不这样做呢?
我想构建我的类,以便test.PartMaster.PIN
向我提供我的CustomParameter类的实例实例。有什么想法吗?
答案 0 :(得分:6)
因为您的类初始化时,PartMaster
类的实例变量在末尾设置了逗号:)
Python解释了这一点:
x = 'test',
为:
('test',)
请改为尝试:
def __init__(self):
self._UUID = None
self.PartNumber = CustomParameter("Part Number", "V_ID")
self.Name = CustomParameter("Name", "V_name")
self.StandardPart = CustomParameter("Standard Part", "V508_isStandardPart")
self.PartType = CustomParameter("Part Type", "V511_PartType")
self.ControlledBy = CustomParameter("ControlledBy", "V511_ControlledBy")
self.PIN = CustomParameter("PIN", "BOECACPinItemNumber")
这发生在我身上很多次。每当我从写出大量字典切换到在这样的类中设置变量时,我总是忘记逗号。但是把头发拉过来一次,你就会知道要找什么!