Python:在循环中创建对象实例
这个程序从一个文件中读取数据,并为文件每一行的数据创建一个叫做 Tunnel 的对象。程序的具体细节不太重要,关键是输出结果能清楚地显示我遇到的问题。
每次我把一个新的 Tunnel(叫做 temp)添加到列表里时,之前创建的所有 Tunnel(也叫 temp,都是在 for 循环之前的迭代中创建的)都会被更改为新的 Tunnel temp。如果这让你感到困惑,请往下看输出结果。
class Tunnel: #I am using the data from the file to create tunnel objects
def __init__ (self,x,y,d):
self.x=x
self.y=y
self.d=d
def __lt__ (self,other):
return self.d<other.d
def __repr__ (self):
return "%s %s" % (str(x),str(y))
file = open("ant.in")
n=int(file.readline())
for i in range(0,n): #this loop is irrelevant
h,t=(int(s) for s in file.readline().split())
tList=[]
mst=[]
for j in range(0,t):
x,y,d=(int(s) for s in file.readline().split())
temp = Tunnel(x,y,d) #I create a new tunnel called "temp"
print(temp) #I print it. It prints as its x and y fields.
tList.append(temp) #I try and append this new tunnel to my list
print(tList) #the list prints all the tunnels, but all the tunnels are changed to the most recent one
程序的输出是
1 2
[1 2]
2 3
[2 3, 2 3]
3 3
[3 3, 3 3, 3 3]
1 4
[1 4, 1 4, 1 4, 1 4]
这个列表应该打印
[1 2, 3 4, 3 3, 1 4]
2 个回答
0
你创建的对象是正确的,但它们的 repr
方法有问题:在 Tunnel 类的 __repr__
方法里,你打印的是 "x" 和 "y" 这两个变量,而不是对象的 self.x
和 self.y
属性。
现在你的代码是这样的:
def __repr__ (self):
return "%s %s" % (str(x),str(y))
这让 Python 去找全局的 x 和 y 变量,而这两个变量正好存在,并且对应的是用来创建最新对象的值。
另外,如果你使用的是 Python 2.x,确保你创建的任何类都要继承自 object
,否则你可能会遇到一些意想不到的行为。
4
这是你的 __repr__
方法——在这里使用 self.x
和 self.y
:
def __repr__ (self):
return "%s %s" % (self.x, self.y)
所以你的代码实际上是可以正常工作的,但打印出来的对象信息不对。它不是打印实例的属性,而是从全局范围打印了 x
和 y
。