ealeon
ealeon

Reputation: 12452

Python importing class Stack

In 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

In 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}

In 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)

I get error in stack.push(w) saying "TypeError: unbound method push() must be called with Stack instance as first argument (got str instance instead)"

What exactly does this mean? I cannot push w? If so, what can I do to push w into the stack?

Upvotes: 3

Views: 4637

Answers (2)

JRK
JRK

Reputation: 182

I think the issue is with the previous line stack = Stack Should be replaced by stack = Stack()

Upvotes: 2

Alex
Alex

Reputation: 44325

You have to initialize Stack properly, I guess you forgot the brackets around:

stack = Stack()

Upvotes: 4

Related Questions