ここにクラス定義があります:
class Graph:
def __init__(self,directional = False,simple=True,Filename=None):
self.adjacencyList = {}
self.directional = directional
self.simple = simple
そして、私はそのための方法を次のように設計__str__
しました:
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
私はそれが非常に冗長であることがわかりました。おそらく、より少ないコード行を使用してそれを行うためのよりクリーンな方法があるのではないかと考えています.
いくつか提案をいただけますか?