So I have a class, called 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))
I want to print a list of objects of this class, like this:
x = [Vertex(1), Vertex(2)]
print x
but it shows me output like this:
[<__main__.Vertex instance at 0xb76ed84c>, <__main__.Vertex instance at 0xb76ed86c>]
Actually, I wanted to print the value of Vertex.label for each object.
Is there any way to do it?
If you just want to print the label for each object, you could use a loop or a list comprehension:
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: