Printing a list of objects of user defined class

前端 未结 3 560
花落未央
花落未央 2020-11-29 06:14

So I have a class, called Vertex.

class Vertex:
    \'\'\'
    This class is the vertex class. It represents a vertex.
    \'\'\'

    def __in         


        
3条回答
  •  粉色の甜心
    2020-11-29 06:58

    If you just want to print the label for each object, you could use a loop or a list comprehension:

    print [vertex.label for vertex in x]
    

    But to answer your original question, you need to define the __repr__ method to get the list output right. It could be something as simple as this:

    def __repr__(self):
        return str(self)
    

提交回复
热议问题