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
  Django "Unexpected Keyword Argument 'min_value'" AstralWeeks 0 79 Yesterday, 04:56 AM
Last Post: AstralWeeks
  __init__() got multiple values for argument 'schema' dawid294 4 1,878 Jan-03-2024, 09:42 AM
Last Post: buran
  Find a specific keyword after another keyword and change the output sgtmcc 5 746 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  "unexpected keyword arg" when initializing my subclasses Phaze90 3 2,966 Nov-25-2022, 07:39 PM
Last Post: Gribouillis
  i want to use type= as a function/method keyword argument Skaperen 9 1,771 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,755 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: missing a required argument: 'y' gible 0 2,843 Dec-15-2021, 02:21 AM
Last Post: gible
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 1,937 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 5,103 May-02-2021, 03:45 PM
Last Post: Anldra12
  TypeError: sum() missing 1 required positional argument: 'num2' Insen 3 5,384 Jan-06-2021, 04:25 PM
Last Post: Insen

Forum Jump:

User Panel Messages

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