0

I define a graph class which has a function for taking a node name and adding the name and node to a dictionary.When I run the program, I receive the error at the bottom. Why does it think I am giving it two arguments?

class Graph:

    def __init__(self):
        self.node_list = {}
        self.number = 0

    def node(node_name):
        if node_name in self.node_list.keys:
            return node_list[node_name]
        else:
            node_list[node_name] = Node()
    ...

def PrefixTrieConstruction(patterns):
    trie = Graph()
    trie.node('root')

for pattern in patterns:
    currentNode = trie.node('root')
    for symbol in pattern:
        for daughter in currentNode.daughters:
            if daughter.label == symbol:
                currentNode = daughter
                break
        else:
            node_name = Trie.name_node()
            Trie.node(node_name)
            Trie.edge(currentNode, node_name, symbol)
            currentNode = node_name
return Trie



Traceback (most recent call last):
  File "PythonProject2.3.py", line 168, in <module>
    main()
  File "PythonProject2.3.py", line 163, in main
    TrieMatching(text, PrefixTrieConstruction(patterns))
  File "PythonProject2.3.py", line 68, in PrefixTrieConstruction
    trie.node('root')
TypeError: node() takes 1 positional argument but 2 were given
jukhamil
  • 779
  • 2
  • 11
  • 18

3 Answers3

4

You are missing self:

def node(self,node_name):

self refers to the instance you are calling the method on so you are already passing the instance so passing 'root' will obviously give you the error you see.

trie.node('root') is basically doing Graph.node(trie,"root") so you are passing two args to a method that takes one, the node_name in your method is being interpreted as the instance so 'root' is an extra argument, unless you are using static or class methods the first arg to your methods should always be self, it can be any name but almost every python programmer by convention uses self to refer to the instance.

difference-between-class-and-instance-methods

what-is-the-difference-between-staticmethod-and-classmethod-in-python

what-is-the-purpose-of-self-in-python

Community
  • 1
  • 1
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
2

That's an easy one ! The function node takes only one parameter, but is located in a class. Given the implicit class instance and your parameter, that's 2 arguments. To fix it, you can edit the function to

     def node(self,node_name):
Jiby
  • 1,865
  • 1
  • 13
  • 22
1

In python, the first argument to a method is always the object on which the method is called. This variable is called self by convention, but there's no rule saying this must be the case. So when you write:

def node(node_name):

node_name is interpreted as the "self" variable and the expected usage of the function is:

trie.node() #within this call, trie is bound to node_name

So when you write:

trie.node('root')

trie is bound to node_name and 'root' is an extra argument, causing an error.

To get your intended behavior, simply declare the self variable:

def node(self, node_name):
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79