My code generates the following error: 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?
The Python "TypeError: Class() takes no arguments" occurs when we forget to define an __init__() method in a class but provide arguments when instantiating it. To solve the error, make sure to define the __init__() (two underscores on each side) method in the class.
__init__ Method with No Parameter __init__ method can be used with no parameter. In this case, the values of the instance will be set to the default values or nothing will be done.
Defining. A function in Python is defined with the def keyword. Functions do not have declared return types. A function without an explicit return statement returns None . In the case of no arguments and no return value, the definition is very simple.
Any class method must have self as first argument. (The name can be any valid variable name, but the name self is a widely established convention in Python.) self represents an (arbitrary) instance of the class.
Your Graph class takes no arguments on __init__
therefore when you call:
graph = Graph(g)
You get an error because Graph doesn't know what to do with '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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With