这个函数的参数是什么?

时间:2017-04-30 20:05:33

标签: python

def push(self,item):
    "Enqueue the 'item' into the queue"
    self.list.insert(0,item)

我在调用此函数时收到TypeError

horizon.push(start)
Python说我需要三个参数,只有两个参数。我冒昧地猜测自我是我拥有的两个中的第二个?但为什么我需要第三个呢?鉴于push()的定义,这没有意义。

我知道这只是我对Python显示的经验不足。有人可以教我这里发生了什么吗?

这是一个班级的Pacman搜索算法练习。我不想问任何可能构成学术不诚实的事情,所以我想尽量减少我发布的代码数量。

Traceback (most recent call last):
  File "pacman.py", line 681, in <module>
    runGames( **args )
  File "pacman.py", line 647, in runGames
    game.run()
  File "/home/user/Documents/search/game.py", line 608, in run
    agent.registerInitialState(self.state.deepCopy())
  File "/home/user/Documents/search/searchAgents.py", line 116, in    registerInitialState
    self.actions  = self.searchFunction(problem) # Find a path
  File "/home/user/Documents/search/search.py", line 105, in uniformCostSearch
    horizon.push(start)
TypeError: push() takes exactly 3 arguments (2 given)

push()是班级PriorityQueue的成员。

再次编辑此内容,之前我错误地发布了课程Queue,而不是PriorityQueue

class PriorityQueue:
    """
      Implements a priority queue data structure. Each inserted item
      has a priority associated with it and the client is usually interested
      in quick retrieval of the lowest-priority item in the queue. This
      data structure allows O(1) access to the lowest-priority item.

      Note that this PriorityQueue does not allow you to change the priority
      of an item.  However, you may insert the same item multiple times with
      different priorities.
    """
    def  __init__(self):
        self.heap = []
        self.count = 0

    def push(self, item, priority):
        # FIXME: restored old behaviour to check against old results better
        # FIXED: restored to stable behaviour
        entry = (priority, self.count, item)
        # entry = (priority, item)
        heapq.heappush(self.heap, entry)
        self.count += 1

    def pop(self):
        (_, _, item) = heapq.heappop(self.heap)
        #  (_, item) = heapq.heappop(self.heap)
        return item

    def isEmpty(self):
        return len(self.heap) == 0

0 个答案:

没有答案