打印用户定义类的对象列表
发布于 2021-01-29 19:10:25
所以我有一堂课,叫做Vertex
。
class Vertex:
'''
This class is the vertex class. It represents a vertex.
'''
def __init__(self, label):
self.label = label
self.neighbours = []
def __str__(self):
return("Vertex "+str(self.label)+":"+str(self.neighbours))
我想打印此类的对象列表,如下所示:
x = [Vertex(1), Vertex(2)]
print x
但是它显示了这样的输出:
[<__main__.Vertex instance at 0xb76ed84c>, <__main__.Vertex instance at 0xb76ed86c>]
实际上,我想打印Vertex.label
每个对象的值。有什么办法吗?
关注者
0
被浏览
50
1 个回答