2014-11-22 44 views
5

我的代碼生成以下錯誤: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()) 

有沒有辦法可以解決這個問題?

回答

11

你Graph類呈現__init__沒有參數,因此,當你調用:

graph = Graph(g)

因爲圖不知道做什麼用「G」你得到一個錯誤。我想你可能想要的是:

class Graph(object):  
    def __init__(self, values): 
     self.__graph_dict = values 
    def vertices(self): 
     return list(self.__graph_dict.keys()) 
相關問題