[英]TypeError: object() takes no parameters
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? 有没有办法解决这个问题?
Your Graph class takes no arguments on __init__
therefore when you call: 因此,当您调用时,您的Graph类不会在__init__
上使用任何参数:
graph = Graph(g) graph =图(g)
You get an error because Graph doesn't know what to do with 'g'. 您收到错误,因为Graph不知道如何处理'g'。 I think what you may want is: 我想你可能想要的是:
class Graph(object):
def __init__(self, values):
self.__graph_dict = values
def vertices(self):
return list(self.__graph_dict.keys())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.