【问题标题】:TypeError: object() takes no parametersTypeError: object() 没有参数
【发布时间】:2014-11-22 14:56:58
【问题描述】:

我的代码产生以下错误: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())

有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: python object parameters


    【解决方案1】:

    您的 Graph 类在 __init__ 上不接受任何参数,因此当您调用时:

    图 = 图(g)

    您收到错误消息,因为 Graph 不知道如何处理“g”。 我想你可能想要的是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      相关资源
      最近更新 更多