gpt4 book ai didi

python - 为什么我的嵌套 python 类实例变成了元组?

转载 作者:太空狗 更新时间:2023-10-30 02:28:26 26 4
gpt4 key购买 nike

我这样定义了一些类:

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 类的实例实例。有什么想法吗?

最佳答案

因为每当您的类初始化时,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")

这在我身上发生过很多次。每当我从编写大量字典转向在类中设置变量时,我总是会忘记逗号。但是,将您的头发拉到上面一次,您将永远知道要寻找什么!

关于python - 为什么我的嵌套 python 类实例变成了元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37062466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com