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

game.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}

search.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 instead)」というエラーが発生します。

これは正確にはどういう意味ですか?押せない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 に答える