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:
JavaScript
x
22
22
1
def Graph():
2
def __init__(self, nodes=None, edges=None, msg="test"):
3
"""
4
assumes that the node and edge lists are the respective objects
5
"""
6
if nodes == None:
7
self.nodes = []
8
else:
9
self.nodes = nodes
10
11
if edges == None:
12
self.edges = []
13
else:
14
self.edges = edges
15
16
self.node_names = []
17
for node in nodes:
18
self.node_names.append(node.get_name())
19
20
self.msg = msg
21
22
(the msg part was for testing the code with the simplest example possible)
What I got was:
JavaScript
1
9
1
g = Graph(msg="33")
2
Traceback (most recent call last):
3
4
File "<ipython-input-29-cc459c9baef3>", line 1, in <module>
5
g = Graph(msg="33")
6
7
TypeError: Graph() got an unexpected keyword argument 'msg'
8
9
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:
.