0

I want to set the type of the parameter like this:

class Tree:
    def __init__(self):
        self.list_of_nodes=[]
    def inser_node (self, node: Tree):
        self.list_of_nodes.append(node)

When I try to do this, the error is raised: NameError: name 'Tree' is not defined

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Naturally, class is not yet in namespace when it's method is being defined. You'll have to use a workaround like using string `"Tree"` instead of class reference. – Tigran Saluev Jun 05 '18 at 15:12

1 Answers1

3

In the same class, you must place the type in quotes:

def inser_node (self, node: 'Tree'):
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80