Skip to main content
 首页 » 编程设计

python之错误 : object() takes no parameters , 无法解决

2025年05月04日14qq号

这是我正在尝试制作 A* 算法的 python 文件,但无法让它工作,我需要一些帮助,它是一个很棒的代码,它已经在 Windows 的最新 python 版本中运行

    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 

我需要知道如何修复它

请您参考如下方法:

类中所有的 init 都是用单下划线而不是双下划线编写的:

将所有写成:_init_的init改成__init__

此外,这一行也不正确:

if _name_ == '__main__': 

名字也需要双下划线

if __name__ == '__main__': 

如果您有兴趣,这里有更多关于为什么需要这样做的信息:

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

请特别查看以下描述:“double_leading_and_trailing_underscore”