2013-02-02 92 views
1

在util.pyPython的導入類堆棧

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:必須使用堆棧實例調用未綁定的方法push()作爲第一個參數(而不是str實例)」

這到底意味着什麼?我不能推w? 如果是這樣,我能做些什麼將w推入堆棧?

回答

3

你必須正確初始化Stack,我想你忘記了周圍的括號:

stack = Stack() 
+0

被替換啊,謝謝你非常。新的python。它現在有效。 – ealeon

2

我認爲這個問題是與前行 stack = Stack 應該 stack = Stack()