将实例添加到列表会创建类似的值

时间:2018-04-03 09:41:10

标签: python python-3.x oop

我有一个需要附加到列表的类的实例。但是,在附加N个实例时,列表的值是最后插入的实例。

这是我尝试做的复制(原始文件很大):

class SomeParent:
  listOfObject = []
  tempObject = None

  def addObjects(self):
    for i in range(10):
      self.tempObject = Child()
      self.changeTempObjectX(i)
      listOfObject.append(self.tempObject)

  def changeTempObjectX(self, i):
      self.tempObject.x = i

class Child:
  x = None

问题:理论上,listOfObject应包含10个具有不同Child属性(0-9)的x类实例。发生的事情是listOfObject(所有这些)的值与最后插入的实例相同。

我正在进行搜索,发现这与可变性和引用有关。我认为正在发生的事情是,插入到列表中的是对前一个值的引用,该值又将指向最后插入的值。

尝试:listOfObject转换为元组,添加操作与此self.listOfObject = self.listOfObject + (self.tempObject,)类似。我也尝试使用所有类型的copy无法正常工作。

我知道应用程序的结构,我不需要更改它,因为需要花费大量精力来重构代码。它非常简单,创建,编辑和保存实例。

如何向列表插入类的实例?

4 个答案:

答案 0 :(得分:0)

问题是x是Child的一个属性。它不是实例的属性。因此,可以通过将x替换为__init__

来简单地解决此问题
class Child:
  def __init__(self):
    self.x = None

答案 1 :(得分:0)

class SomeParent:
  def __init__(self):
    self.listOfObject = []
    self.tempObject = None

  def addObjects(self):
    for i in range(10):
      self.tempObject = Child()
      self.changeTempObjectX(i)
      self.listOfObject.append(self.tempObject)

  def changeTempObjectX(self, i):
      self.tempObject.x = i

  def output(self):
      for i in range(10):
          print self.listOfObject[i].x

class Child:
  x = None

sp = SomeParent()
sp.addObjects()
sp.output()

我修复了你的代码。你的代码无效。最好在下次测试你的最小例子。所以我不能指出你的问题在哪里,因为你的例子不起作用。但检查我的固定版本,输出是正确的。你会得到不同的东西。

答案 2 :(得分:0)

尝试执行此操作 -

class SomeParent:
  listOfObject = []
  tempObject = None

  def addObjects(self):
    for i in range(10):
      self.tempObject = Child()
      self.changeTempObjectX(i)
      self.listOfObject.append(self.tempObject)

  def changeTempObjectX(self, i):
      self.tempObject.x = i

class Child:
  x = None

testObject = SomeParent()
testObject.addObjects()
for each in testObject.listOfObject:
    print each.x

答案 3 :(得分:0)

您错过了在listOfObject之前添加自我。优良作法是使用初始化属性的__init__函数。您可以尝试以下程序。

class SomeParent(object):
  def __init__(self):
    self.listOfObject = []
    self.tempObject = None    
  def addObjects(self):
    for i in range(10):
      self.tempObject = Child()
      self.changeTempObjectX(i)
      self.listOfObject.append(self.tempObject)   # ==> Line where you missed self

  def changeTempObjectX(self, i):
      self.tempObject.x = i

class Child(object):
  def __init__(self):
    self.x = None

sp = SomeParent()
sp.addObjects()
print(sp.listOfObject)

listOfObject = sp.listOfObject[:]

for child_object in listOfObject:
  print(child_object.x)

输出:

[<Child object at 0x7fdc0e13e438>, <Child object at 0x7fdc0e159be0>, 
 <Child object at 0x7fdc0e159c18>, <Child object at 0x7fdc0e159c50>, 
 <Child object at 0x7fdc0e159c88>, <Child object at 0x7fdc0e159cc0>, 
 <Child object at 0x7fdc0e159cf8>, <Child object at 0x7fdc0e159d30>, 
 <Child object at 0x7fdc0e159d68>, <Child object at 0x7fdc0e159da0>]
0
1
2
3
4
5
6
7
8
9

如果你在listOfObject之前加入了自我,那么你的程序是正确的,不应该给你相同的值,你应该从0到9的5个值。我运行你的程序

class SomeParent:
  listOfObject = []
  tempObject = None

  def addObjects(self):
    for i in range(10):
      self.tempObject = Child()
      self.changeTempObjectX(i)
      self.listOfObject.append(self.tempObject)  # ==> Line where you 

  def changeTempObjectX(self, i):
      self.tempObject.x = i

class Child:
  x = None

sp = SomeParent()
sp.addObjects()
print(sp.listOfObject)


for child_object in sp.listOfObject:
  print(child_object.x)

输出:

[<Child object at 0x7f713ad220f0>, <Child object at 0x7f713ad227f0>, 
 <Child object at 0x7f713ad22320>, <Child object at 0x7f713ad22470>, 
 <Child object at 0x7f713a9d6198>, <Child object at 0x7f713a9d6e48>, 
 <Child object at 0x7f713a9d6e80>, <Child object at 0x7f713a9c7c88>, 
 <Child object at 0x7f713a9c7cc0>, <Child object at 0x7f713a9d74e0>]
0
1
2
3
4
5
6
7
8
9