Python Forum
TypeError: __init__() got an unexpected keyword argument 'value' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: TypeError: __init__() got an unexpected keyword argument 'value' (/thread-33602.html)



TypeError: __init__() got an unexpected keyword argument 'value' - Anldra12 - May-10-2021

Quote:This is overall code it return me an error TypeError: __init__() got an unexpected keyword argument 'value'
where return the general tree to find shortest path with and without siblings) from the root to the node, c1.
The follow error this is overall codes

Error:
TypeError Traceback (most recent call last) <ipython-input-77-6f90865f60cf> in <module> 220 221 if __name__ == '__main__': --> 222 a1 = GeneralTreeNode(value='a1') 223 b1 = GeneralTreeNode(value='b1') 224 b2 = GeneralTreeNode(value='b2') TypeError: __init__() got an unexpected keyword argument 'value
'

class GeneralTree():
    
    def __init__(self,root=None):
        self.root = root
        self.first_born = self.root.get_child()        
        self.current_node = self.first_born
        self.current_value = self.current_node.get_value()
        self.start = self.current_value
        self.visited = LinkedList(self.root.get_value())
        self.path = LinkedList(self.root.get_value())
        self.child_path = LinkedList(self.root.get_value())
        
    def check_visited(self,val):
        if self.visited.find(val):
            return True
        else:
            return False
        
    def check_child_path(self,val):
        if self.child_path.find(val):
            return True
        else:
            return False        
        
        
    def depth_first_traversal(self):

        if self.current_value == self.root.get_value():
            self.visited.insert_at(idx=1,val=self.start)
            
            # copy self.visited then reset for future method calls
            self.completed_visited = self.visited
            self.current_node = self.first_born
            self.current_value = self.current_node.get_value()
            self.start = self.current_value
            self.visited = LinkedList(self.root.get_value())
            
            return self.completed_visited.dump_list() 
        
        else:
            # ==== tree traversal logic ====
            if self.current_node.get_child() and self.check_visited(self.current_node.get_child().get_value()) == False:
                # parent -> child (not yet visited)
                self.current_node = self.current_node.get_child()
                self.current_value = self.current_node.get_value()
                self.visited.append(self.current_value)                  

            elif self.current_node.get_right() and self.check_visited(self.current_node.get_right().get_value()) == False:            
                # sibling -> right_sibling (not yet visited)
                self.current_node = self.current_node.get_right()
                self.current_value = self.current_node.get_value() 
                self.visited.append(self.current_value)                                           
                
            elif self.current_node.get_right() == None and self.current_node.get_left():
                # right_most_sibling -> left_sibling (already visited)
                self.current_node = self.current_node.get_left()
                self.current_value = self.current_node.get_value()
                
            elif self.current_node.get_left() != None and self.check_visited(self.current_node.get_right().get_value()) == True:
                # sibling (not left-most or right-most) -> left_sibling (already visited)
                self.current_node = self.current_node.get_left()
                self.current_value = self.current_node.get_value()
                
            else:
                # left_most_sibling -> parent (already visited)
                self.current_node = self.current_node.get_parent()
                self.current_value = self.current_node.get_value()               
                
            # ==== recursively apply logic ====
            self.depth_first_traversal()
            
            
    def depth_first_search(self,search_val):
        self.search_val = search_val

        if self.current_value == search_val or self.current_value == self.root.get_value():
            self.visited.insert_at(idx=1,val=self.start)
            self.path.insert_at(idx=1,val=self.start)
            
            
            if self.check_visited(self.search_val) == True:
                condition = 1
            else:
                condition = 0
            
            # copy self.path and reset for future method calls
            self.completed_path = self.path      
            self.current_node = self.first_born
            self.current_value = self.current_node.get_value()
            self.start = self.current_value
            self.visited = LinkedList(self.root.get_value())
            self.path = LinkedList(self.root.get_value())
            
            if condition == 1:
                return self.completed_path.dump_list() 
            else:
                print("Value not found")
        
        else:
            # ==== tree traversal logic ====
            if self.current_node.get_child() and self.check_visited(self.current_node.get_child().get_value()) == False:
                # parent -> child (not yet visited)
                self.current_node = self.current_node.get_child()
                self.current_value = self.current_node.get_value()
                self.visited.append(self.current_value)
                self.path.append(self.current_value)                    

            elif self.current_node.get_right() and self.check_visited(self.current_node.get_right().get_value()) == False:            
                # sibling -> right_sibling (not yet visited)
                self.current_node = self.current_node.get_right()
                self.current_value = self.current_node.get_value() 
                self.visited.append(self.current_value)                    
                self.path.append(self.current_value)                         
                
            elif self.current_node.get_right() == None and self.current_node.get_left():
                # right_most_sibling -> left_sibling (already visited)
                self.current_node = self.current_node.get_left()
                self.current_value = self.current_node.get_value()
                
            elif self.current_node.get_left() != None and self.check_visited(self.current_node.get_right().get_value()) == True:
                # sibling (not left-most or right-most) -> left_sibling (already visited)
                self.current_node = self.current_node.get_left()
                self.current_value = self.current_node.get_value()
                self.path.deleteAt(idx=self.path.count)
                
            else:
                # left_most_sibling -> parent (already visited)
                self.current_node = self.current_node.get_parent()
                self.current_value = self.current_node.get_value() 
                self.path.deleteAt(idx=self.path.count)                
                
            # ==== recursively apply logic ====
            self.depth_first_search(search_val=self.search_val)           
            
            
    def child_depth_first_search(self,search_val):
        self.search_val = search_val

        if self.current_value == search_val or self.current_value == self.root.get_value():
            self.visited.insert_at(idx=1,val=self.start)
            self.path.insert_at(idx=1,val=self.start)         
            
            
            if self.check_visited(self.search_val) == True:
                condition = 1
            else:
                condition = 0
            
            # copy self.path and reset for future method calls
            self.completed_child_path = self.child_path            
            self.current_node = self.first_born
            self.current_value = self.current_node.get_value()
            self.start = self.current_value
            self.visited = LinkedList(self.root.get_value())
            self.child_path = LinkedList(self.root.get_value())            
            
            if condition == 1:
                return self.completed_child_path.dump_list() 
            else:
                print("Value not found")
        
        else:
            # ==== tree traversal logic ====
            if self.current_node.get_child() and self.check_visited(self.current_node.get_child().get_value()) == False:
                # parent -> child (not yet visited)
                if self.check_child_path(self.current_node.get_value()) == False:
                    self.child_path.append(self.current_value)                  
                self.current_node = self.current_node.get_child()
                self.current_value = self.current_node.get_value()
                self.visited.append(self.current_value) 
                self.child_path.append(self.current_value)                  

            elif self.current_node.get_right() and self.check_visited(self.current_node.get_right().get_value()) == False:            
                # sibling -> right_sibling (not yet visited)
                self.child_path.deleteAt(idx=self.child_path.count)
                self.current_node = self.current_node.get_right()
                self.current_value = self.current_node.get_value() 
                self.visited.append(self.current_value)                                  
                self.child_path.append(self.current_value)
                
            elif self.current_node.get_right() == None and self.current_node.get_left():
                # right_most_sibling -> left_sibling (already visited)
                self.current_node = self.current_node.get_left()
                self.current_value = self.current_node.get_value()
                
            elif self.current_node.get_left() != None and self.check_visited(self.current_node.get_right().get_value()) == True:
                # sibling (not left-most or right-most) -> left_sibling (already visited)
                self.current_node = self.current_node.get_left()
                self.current_value = self.current_node.get_value()
                self.child_path.deleteAt(idx=self.child_path.count)                
                
            else:
                # left_most_sibling -> parent (already visited)
                self.current_node = self.current_node.get_parent()
                self.current_value = self.current_node.get_value() 
                self.child_path.deleteAt(idx=self.child_path.count)                 
                
            # ==== recursively apply logic ====
            self.child_depth_first_search(search_val=self.search_val)            
            
if __name__ == '__main__':
  a1 = GeneralTreeNode(value='a1')
  b1 = GeneralTreeNode(value='b1')
  b2 = GeneralTreeNode(value='b2')
  b3 = GeneralTreeNode(value='b3')
  a1.set_child(b1)
  b1.set_parent(a1)
  b1.set_right(b2)
  b2.set_left(b1)
  b2.set_right(b3)
  b3.set_left(b2)

  c1 = GeneralTreeNode(value='c1')
  c1.set_parent(b3)
  b3.set_child(c1)

  d1 = GeneralTreeNode(value='d1')
  d1.set_parent(b1)
  b1.set_child(d1)
  
  r = GeneralTree(root=a1)
  r.depth_first_search(search_val='c1')
  r.child_depth_first_search(search_val='c1')



RE: TypeError: __init__() got an unexpected keyword argument 'value' - buran - May-10-2021

The signature of GeneralTree.__init__ is
def __init__(self,root=None)
The parameter (apart from self) is root, nothing else.


We don't see the code for GeneralTreeNode class


RE: TypeError: __init__() got an unexpected keyword argument 'value' - deanhystad - May-10-2021

When you call GeneralTreeNode(value='a1') this creates a new GeneralTreeNode object and calls GeneralTreeNode.__init__(value='a1'). 'value' does not appear in the __init__ arguments.
 def __init__(self,root=None):



RE: TypeError: __init__() got an unexpected keyword argument 'value' - buran - May-10-2021

@deanhystad - that is the signature of GeneralTree.__init__(). I've made the same mistake initially


RE: TypeError: __init__() got an unexpected keyword argument 'value' - Anldra12 - May-10-2021

(May-10-2021, 02:41 PM)deanhystad Wrote: When you call GeneralTreeNode(value='a1') this creates a new GeneralTreeNode object and calls GeneralTreeNode.__init__(value='a1'). 'value' does not appear in the __init__ arguments.
 def __init__(self,root=None):
Tell me the exact problem and solution I don't know even this case how to do there are different value for general tree where a1 represent root
you mean pass value in the argument def __init(self,root,value,etc)


RE: TypeError: __init__() got an unexpected keyword argument 'value' - Gribouillis - May-10-2021

What is the code of the GeneralTreeNode class, especially its __init__ method ?


RE: TypeError: __init__() got an unexpected keyword argument 'value' - Anldra12 - May-11-2021

@Gribouillis yes GeneralTreeNode class __init__() for string type linked list under child path but still get an error


RE: TypeError: __init__() got an unexpected keyword argument 'value' - deanhystad - May-11-2021

You aren't getting any help here until you provide the requested GeneralTreeNode code,