Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: object() takes no parameters

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?

like image 942
Riccardo moroletti Avatar asked Nov 22 '14 14:11

Riccardo moroletti


People also ask

Why does my class take no arguments?

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.

What is meant by init method without parameters?

__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.

Which is the definition of function called if it does nothing and takes no arguments in Python?

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.

Do Python classes take arguments?

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.


1 Answers

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())
like image 51
Arthur.V Avatar answered Oct 17 '22 19:10

Arthur.V