Skip to content
Advertisement

Python: Why do I get an “unexpected keyword argument” error? [closed]

I am trying to set up a graph. For the initialization, I wanted the option of either starting with a collection of nodes and edges or not. So I gave them the default value None. Or so I thought:

def Graph():
    def __init__(self, nodes=None, edges=None, msg="test"):
        """
        assumes that the node and edge lists are the respective objects
        """
        if nodes == None:
            self.nodes = []
        else:
            self.nodes = nodes
            
        if edges == None:
            self.edges = []
        else:
            self.edges = edges
        
        self.node_names = []
        for node in nodes:
            self.node_names.append(node.get_name())
            
        self.msg = msg

(the msg part was for testing the code with the simplest example possible)

What I got was:

g = Graph(msg="33")
Traceback (most recent call last):

  File "<ipython-input-29-cc459c9baef3>", line 1, in <module>
    g = Graph(msg="33")

TypeError: Graph() got an unexpected keyword argument 'msg'

Can anybody help me? It’s probably a ridiculously simply thing, but I just don’t see it, and I’m going slightly mad here…

Advertisement

Answer

You’ve defined Graph not as a class but as a regular function.

Replace def Graph(): with class Graph:.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement