Python 类型错误:object() 不带参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27078742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:24:41  来源:igfitidea点击:

TypeError: object() takes no parameters

pythonobjectparameters

提问by Riccardo moroletti

My code generates the following error: TypeError: object() takes no parameters

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

Is there a way I can solve this?

有没有办法解决这个问题?

采纳答案by Arthur.V

Your Graph class takes no arguments on __init__therefore when you call:

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

graph = Graph(g)

图 = 图(g)

You get an error because Graph doesn't know what to do with 'g'. I think what you may want is:

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

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