gpt4 book ai didi

Python 导入类 Stack

转载 作者:太空宇宙 更新时间:2023-11-04 09:11:22 24 4
gpt4 key购买 nike

在 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:未绑定(bind)方法 push() 必须使用 Stack 实例作为第一个参数调用(取而代之的是 str 实例)”

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

最佳答案

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

stack = Stack()

关于Python 导入类 Stack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14659916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com