我的代码生成以下错误:TypeError: object() takes no parameters
class Graph(object):
def vertices(self):
return list(self.__graph_dict.keys())
if __name__ == "__main__":
g = { "a" : ["d"],
"b" : ["c"],
"c" : ["b", "c", "d", "e"],
"d" : ["a", "c"],
"e" : ["c"],
"f" : []
}
graph = Graph(g)
print("Vertices of graph:")
print(graph.vertices())
我有办法解决这个问题吗?
答案 0 :(得分:14)
您的Graph类在__init__
上不带任何参数,因此当您致电:
graph = Graph(g)
您收到错误,因为Graph不知道如何处理' g'。 我想你可能想要的是:
class Graph(object):
def __init__(self, values):
self.__graph_dict = values
def vertices(self):
return list(self.__graph_dict.keys())