Python Forum
TypeError: __init__() got an unexpected keyword argument 'value'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: __init__() got an unexpected keyword argument 'value'
#1
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')
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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):
Reply
#4
@deanhystad - that is the signature of GeneralTree.__init__(). I've made the same mistake initially
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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)
Reply
#6
What is the code of the GeneralTreeNode class, especially its __init__ method ?
Reply
#7
@Gribouillis yes GeneralTreeNode class __init__() for string type linked list under child path but still get an error
Reply
#8
You aren't getting any help here until you provide the requested GeneralTreeNode code,
buran likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 534 Aug-06-2021, 08:05 PM
Last Post: SheeppOSU
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 1,708 May-02-2021, 03:45 PM
Last Post: Anldra12
  TypeError: sum() missing 1 required positional argument: 'num2' Insen 3 1,746 Jan-06-2021, 04:25 PM
Last Post: Insen
  TypeError: forward() missing 1 required positional argument: 'x' sveto4ka 4 4,766 Jun-17-2020, 07:25 PM
Last Post: sveto4ka
  TypeError: _linspace_dispatcher() missing 1 required positional argument: 'stop' Ae_Lovecraft 3 3,266 May-28-2020, 03:33 PM
Last Post: Larz60+
  TypeError: coerce_kw_type() got an unexpected keyword argument 'dest' juniii 1 1,063 Apr-24-2020, 01:53 PM
Last Post: Jeff900
  SyntaxError: positional argument follows keyword argument syd_jat 3 3,496 Mar-03-2020, 08:34 AM
Last Post: buran
  TypeError: int() argument must be a string or a number, not 'NoneType' Hadad 2 4,535 Jul-30-2019, 07:22 PM
Last Post: Hadad
  Super with Sublime Text - TypeError: super() takes at least 1 argument (0 given) Shafla 6 3,894 May-04-2019, 08:30 PM
Last Post: Shafla
  Unexpected output, TypeError and traceback error fier259 2 1,842 May-06-2018, 10:52 PM
Last Post: fier259

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020