Search code examples
pythona-star

Error: object() takes no parameters , cant resolve it


this is python file im trying to make A*algorithm , but cant get it to work, I need some help , its an awesome code , its been run in latest python version for windows

    from queue import PriorityQueue
    class State(object):
        def _init_(self,value,parent,start = 0,goal = 0):
            self.children = []
            self.value = value
            self.parent = parent
            self.dist = 0
            if parent:
                self.path = parent.path[:]
                self.path.append(value)
                self.start = parent.start
                self.goal = parent.goal
            else:
                self.path = [value]
                self.start = start
                self.goal = goal


        def GetDist(self):
            pass

        def CreateChildren(self):
            pass


    class State_String(State):
        def _init_(self,value,parent,start = 0,goal = 0):
            super(State_String,self).__init__(value,parent,start,goal)
            self.dist = self.GetDist()
        def GetDist(self):
            if self.value == self.goal:
                return 0
            dist = 0
            for i in range(len(self.goal)):
                letter = self.goal[i]
                dist += abs(i - self.value.index(letter))
            return dist

        def CreateChildren(self):
            if not self.children:
                for i in xrange(len(self.goal)-1):
                    val = self.value
                    val = val[:i] + val[i+1] + val[i] + val[i+2:]
                    child = State_String(val,self)
                    self.children.append(child)


    class AStar_Solver:
        def _init_(self,start,goal):
            self.path = []
            self.visitedQueue = []
            self.priorityQueue = PriorityQueue()
            self.start = start
            self.goal = goal

        def Solve(self):
            startState = State_String(self.start,0,self.start,self.goal)
            count = 0
            self.priorityQueue.put((0,count,startState))
            while(not self.path and self.priorityQueue.qsize()):
                closestChild = self.priorityQueue.get()[2]
                closestChild.CreateChildren()
                self.visitedQueue.append(closestChild.value)
                for child in closestChild.children:
                    if child.value not in self.visitedQueue:
                        count +=1
                        if not child.dist:
                            self.path = child.path
                            break
                        self.priorityQueue.put((child.dist,count,child))

            if not self.path:
                print "Goal of " + self.goal + " is not possible!"
            return self.path

    if _name_ == '__main__':
        start1 = "hma"
        goal1 = "ham"
        a = AStar_Solver(start1,goal1)
        a.Solve()
        for i in xrange(len(a.path)):
            print " %d)" %i + a.path[i]

getting these errors:

    Traceback (most recent call last):
      File "C:/Users/Herby/Desktop/untitled/Astar.py", line 82, in <module>
        a.Solve()
      File "C:/Users/Herby/Desktop/untitled/Astar.py", line 59, in Solve
        startState = State_String(self.start,0,self.start,self.goal)
    TypeError: object() takes no parameters

I need to know how it can be fixed


Solution

  • All of your init in your classes are written with single underscore instead of double underscore:

    Change all init written as: _init_ to __init__

    Also, this line is incorrect as well:

    if _name_ == '__main__':
    

    It needs to be double underscore for name as well

    if __name__ == '__main__':
    

    If you're interested, here is more information on why this is needed:

    https://www.python.org/dev/peps/pep-0008/

    In particular look at the description for: "double_leading_and_trailing_underscore"