我是Python的新手,正在编写一个简单的Stack类。请查看下面的代码-
class Stack:
def __int__(self):
self.mylist = []
def push(self, element):
self.mylist.append(element)
def pop(self):
return self.mylist.pop()
myStack = Stack()
myStack.push(1)
但是,当我运行此简单程序时,出现以下错误-
C:\Users\devshankhasharm\PycharmProjects\DataTypes\venv\Scripts\python.exe C:/Users/devshankhasharm/PycharmProjects/DataTypes/venv/stack.py
Traceback (most recent call last):
File "C:/Users/devshankhasharm/PycharmProjects/DataTypes/venv/stack.py", line 13, in <module>
myStack.push(1)
File "C:/Users/devshankhasharm/PycharmProjects/DataTypes/venv/stack.py", line 6, in push
self.mylist.append(element)
AttributeError: 'Stack' object has no attribute 'mylist'
不确定为什么会抱怨mylist属性。可能是一些缩进问题吗?我什至在该文件上运行了reindent.py,但问题仍然无法解决。感谢您的帮助
答案 0 :(得分:0)
类中的初始化方法名为__init__
就这么简单!