我正在尝试创建一个类的多个实例,该实例正在另一个类中的多个例子中使用,如下代码所示。
我只想在python中使用"nodeInfo"类作为数据类型,为初始化它的任何实例存储不同的值,因此很容易在由另一个类"Build"形成的其他实例中使用。
然而,在代码中,变量"a"one_answers"b"的"test1"、"test2"one_answers"test3"的实例共享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]}
当在NodeInfo.__init__
之外声明itemValues
时,您正在创建一个将由所有NodeInfo
实例共享的类属性。
如果希望NodeInfo.itemValues
是一个实例属性,每个实例都有唯一的值(对象),则需要在__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)
用法:
>>> test = Build('a')
>>> test2 = Build('b')
>>> print '{}:{} --- {}:{}'.format(test.v, test.a, test2.v, test2.a)
a:{'attributes': [1, 2, 3], 'values': [1, 2, 3]} --- b:{'attributes': [1, 2, 3], 'values': [1, 2, 3]}
>>>
>>> test2.setItem(['x', 'y', 'z'], ['c', 'b', 'a'])
>>>
>>> print '{}:{} --- {}:{}'.format(test.v, test.a, test2.v, test2.a)
a:{'attributes': [1, 2, 3], 'values': [1, 2, 3]} --- b:{'attributes': ['x', 'y', 'z'], 'values': ['c', 'b', 'a']}
>>>
看看Python教程-9。课程。当你达到9.3的结尾时,你会发现你的问题有一个类似的例子。建议您阅读整个章节,9、9.1和9.2是Python的重要概念。
从构建中删除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]}调用nodeInfo类的构造函数{‘attributes’:[3,2,1],‘values’:[3],2,1]}{‘Attributetes’:[3]、2、1],‘value’:[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