相关疑难解决方法(0)

打印用户定义的类的对象列表

所以我有一个叫做的课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))
Run Code Online (Sandbox Code Playgroud)

我想打印这个类的对象列表,如下所示:

x = [Vertex(1), Vertex(2)]
print x
Run Code Online (Sandbox Code Playgroud)

但它显示我这样的输出:

[<__main__.Vertex instance at 0xb76ed84c>, <__main__.Vertex instance at 0xb76ed86c>]
Run Code Online (Sandbox Code Playgroud)

实际上,我想打印Vertex.label每个对象的值.有什么办法吗?

python

51
推荐指数
2
解决办法
5万
查看次数

标签 统计

python ×1