2

在 util.py

class Stack:
  "A container with a last-in-first-out (LIFO) queuing policy."
  def __init__(self):
    self.list = []

  def push(self,item):
    "Push 'item' onto the stack"
    self.list.append(item)

  def pop(self):
    "Pop the most recently pushed item from the stack"
    return self.list.pop()

  def isEmpty(self):
    "Returns true if the stack is empty"
    return len(self.list) == 0

在游戏.py

class Directions:
  NORTH = 'North'
  SOUTH = 'South'
  EAST = 'East'
  WEST = 'West'
  STOP = 'Stop'

  LEFT =       {NORTH: WEST,
                 SOUTH: EAST,
                 EAST:  NORTH,
                 WEST:  SOUTH,
                 STOP:  STOP}

  RIGHT =      dict([(y,x) for x, y in LEFT.items()])

  REVERSE = {NORTH: SOUTH,
             SOUTH: NORTH,
             EAST: WEST,
             WEST: EAST,
             STOP: STOP}

在搜索.py

  from game import Directions
  s = Directions.SOUTH
  w = Directions.WEST
  e = Directions.EAST
  n = Directions.NORTH

  from util import Stack
  stack = Stack
  stack.push(w)

我在 stack.push(w) 中收到错误说“TypeError: unbound method push() must be called with Stack instance as first argument (got str instance)”

这到底是什么意思?我不能推w?如果是这样,我该怎么做才能将 w 推入堆栈?

4

2 回答 2

4

你必须Stack正确初始化,我猜你忘记了括号:

stack = Stack()
于 2013-02-02T08:08:35.507 回答
2

我认为问题出在上一行 stack = Stack 应该替换为 stack = Stack()

于 2013-02-02T08:15:30.393 回答