class Stack(): def __init__(self): self.element = [] # 入栈 def push(self,item): self.element.append(item) # 出栈 def pop(self): if self.element: self.element.pop() else: raise LookupError('stack is error') # 判空 def is_empty(self): return bool(self.element) # 返回栈顶元素 def top(self): return self.element[-1] if __name__ == '__main__': stack = Stack() print(stack.is_empty()) stack.push(1) stack.push(2) stack.push(3) print(stack.element) stack.pop() print(stack.element) print(stack.is_empty()) print(stack.top())
运行结果: