为了学习python和一般的一些数据结构,我首先要创建一个堆栈。这是一个简单的基于数组的堆栈。你知道吗
这是我的密码:
class ArrayBasedStack:
'Class for an array-based stack implementation'
def __init__(self):
self.stackArray = []
def pop():
if not isEmpty():
# pop the stack array at the end
obj = self.stackArray.pop()
return obj
else:
print('Stack is empty!')
return None
def push(object):
# push to stack array
self.stackArray.append(object)
def isEmpty():
if not self.stackArray: return True
else: return False
'''
Testing the array-based stack
'''
abs = ArrayBasedStack()
abs.push('HI')
print(abs.pop())
但我得到了一个错误:
Traceback (most recent call last):
File "mypath/arrayStack.py", line 29, in abs.push('HI')
TypeError: push() takes 1 positional argument but 2 were given [Finished in 0.092s]
缺少
self
参数。你知道吗self
是对对象的引用。在许多C
风格的语言中,它非常接近这个概念。你知道吗所以你可以这样修理。你知道吗
输出:
相关问题 更多 >
编程相关推荐