错误:object()不带参数,无法解析它

时间:2015-10-15 01:13:32

标签: python a-star

这是python文件我试图制作A *算法,但不能让它工作,我需要一些帮助,它是一个很棒的代码,它已经在最新的python版本的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

我需要知道如何修复它

1 个答案:

答案 0 :(得分:1)

您班级中的所有git fetch都使用单下划线而非双下划线编写:

将所有写为:git rebase的初始更改为init

此外,此行也不正确:

_init_

名称也需要双下划线

__init__

如果您有兴趣,可以在此处获取有关为何需要这些内容的更多信息:

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

请特别查看以下说明:“ double_leading_and_trailing_underscore