I have a class definition here:
class Graph:
def __init__(self,directional = False,simple=True,Filename=None):
self.adjacencyList = {}
self.directional = directional
self.simple = simple
and I designed __str__
method for it like this:
def __str__(self):
simple = "Simple: "+ str(self.simple)+"\n"
directional = "Directional: " + str(self.directional)+"\n"
items = "{\n"
for vertex in self.adjacencyList.keys():
items = items +"\t"+str(vertex)+str(self.adjacencyList[vertex])+"\n"
items += "}"
string = simple + directional + items
return string
I found it is so verbose and I am thinking maybe there is some cleaner way to do it using fewer lines of code.
Can you give me some suggestions?