我正在尝试创建一个类的多个实例,这个实例正在另一个类的多个实例中使用,如下面的代码。
我想使用' nodeInfo' class只作为python中的数据类型,为初始化它的任何实例存储不同的值,因此很容易在其他类中构建的其他实例中使用#' Build'。
但是,在代码中,' test1',' test2'的实例和' test3'变量' a'和' b'与nodeInfo类共享相同的值。
你能帮助我使用类nodeInfo作为数据类型吗?所以我可以存储不同的值?
我希望我的问题很明确。非常感谢您的帮助。
class nodeInfo(object):
itemValues = {
"attributes" : [],
"values" : []
}
def __init__(self, attributes, values):
print "calling constructor of nodeInfo Class"
self.itemValues["attributes"] = attributes
self.itemValues["values"] = values
class Build(object):
v = ''
a = nodeInfo
b = nodeInfo
def __init__(self, name):
a = nodeInfo([1,2,3], [1,2,3])
b = nodeInfo([1,2,3], [1,2,3])
v = name
def setItem(self, attribute, value):
self.a = nodeInfo(attribute, value)
test = Build("a")
test2 = Build("b")
test3 = Build("c")
print test.a.itemValues, test2.a.itemValues, test3.a.itemValues
#test.setItem([3,2,1], [3,2,1])
#test2.a.itemValues = nodeInfo([3,2,1], [3,2,1])
test3.setItem([3,2,1], [3,2,1])
print test.a.itemValues, test2.a.itemValues, test3.a.itemValues
结果:
calling constructor of nodeInfo Class
{'attributes': [1, 2, 3], 'values': [1, 2, 3]} {'attributes': [1, 2, 3], 'values': [1, 2, 3]} {'attributes': [1, 2, 3], 'values': [1, 2, 3]}
calling constructor of nodeInfo Class
{'attributes': [3, 2, 1], 'values': [3, 2, 1]} {'attributes': [3, 2, 1], 'values': [3, 2, 1]} {'attributes': [3, 2, 1], 'values': [3, 2, 1]}
应该是:
calling constructor of nodeInfo Class
{'attributes': [1, 2, 3], 'values': [1, 2, 3]} {'attributes': [1, 2, 3], 'values': [1, 2, 3]} {'attributes': [1, 2, 3], 'values': [1, 2, 3]}
calling constructor of nodeInfo Class
{'attributes': [1, 2, 3], 'values': [1, 2, 3]} {'attributes': [1, 2, 3], 'values': [1, 2, 3]} {'attributes': [3, 2, 1], 'values': [3, 2, 1]}
答案 0 :(得分:0)
从构建中删除a和b
的声明当您声明类似的类成员时,它们被视为静态成员并在所有类实例之间共享
那么你的构造函数必须引用a和b作为self.a和self.b,否则它们将被视为局部变量
编辑:
class Build(object):
def __init__(self, name):
self.a = nodeInfo([1,2,3], [1,2,3])
self.b = nodeInfo([1,2,3], [1,2,3])
self.v = name
def setItem(self, attribute, value):
self.a = nodeInfo(attribute, value)
def getItemA(self):
return self.a
def getItemB(self):
return self.b
=============================================== ===
谢谢你。这是实际结果&代码,它认为相同的nodeInfo是静态的。我发现如果我没有使用nodeInfo并使用dict创建一个实例来构建。它不是用作静态的,但在这种情况下,我们松开已经定义的数据类型。
结果: {'attributes':[1,2,3],'values':[1,2,3]} {'attributes':[1,2,3],'values':[1,2,3]} {'attributes':[1,2,3],'values':[1,2,3]} 调用nodeInfo类的构造函数 {'attributes':[3,2,1],'values':[3,2,1]} {'attributes':[3,2,1],'values':[3,2,1]} {'attributes':[3,2,1],'values':[3,2,1]}
代码:
__author__ = 'j'
class nodeInfo(object):
itemValues = {
"attributes" : [],
"values" : []
}
def __init__(self, attributes, values):
print "calling constructor of nodeInfo Class"
self.itemValues["attributes"] = attributes
self.itemValues["values"] = values
class Build(object):
def __init__(self, name):
self.a = nodeInfo([1,2,3], [1,2,3])
self.b = nodeInfo([1,2,3], [1,2,3])
self.v = name
def setItem(self, attribute, value):
self.a = nodeInfo(attribute, value)
def getItemA(self):
return self.a
def getItemB(self):
return self.b
test = Build(1)
test2 = Build(2)
test3 = Build(3)
test.setItem([1,2,3], [1,2,3])
print test.getItemA().itemValues, test2.getItemA().itemValues, test3.getItemA().itemValues
test3.setItem([3,2,1], [3,2,1])
print test.getItemA().itemValues, test2.getItemA().itemValues, test3.getItemA().itemValues
答案 1 :(得分:0)
当您在itemValues
之外声明 NodeInfo.__init__
时,您正在创建一个将由所有NodeInfo
个实例共享的类属性。
如果您希望NodeInfo.itemValues
成为每个实例的唯一值(对象)的实例属性,则需要在{{>中创建 1}}方法。
__init__
要为实例属性指定值,您需要使用class NodeInfo(object):
def __init__(self, attributes, values):
# print "calling constructor of NodeInfo Class"
self.itemValues = dict()
self.itemValues["attributes"] = attributes
self.itemValues["values"] = values
def __str__(self):
return str(self.itemValues)
。您在self.attribute = ...
。
Build.__init__
用法:
class Build(object):
def __init__(self, name):
self.a = NodeInfo([1,2,3], [1,2,3])
self.b = NodeInfo([1,2,3], [1,2,3])
self.v = name
def setItem(self, attribute, value):
self.a = NodeInfo(attribute, value)
浏览Python Tutorial - 9. Classes。当你到了9.3的末尾,你会发现一个类似的问题例子。建议你阅读整个部分,9,9.1和9.2是Python的重要概念。