Я исправил несколько проблем ниже. Кроме того, «стек» в абстрактных терминах программирования обычно представляет собой коллекцию, в которую вы добавляете и удаляете сверху, но способ, которым вы ее реализовали, вы добавляете в начало и удаляете снизу, что делает его очередью ,
class myStack:
def __init__(self):
self.container = [] # You don't want to assign [] to self - when you do that, you're just assigning to a new local variable called `self`. You want your stack to *have* a list, not *be* a list.
def isEmpty(self):
return self.size() == 0 # While there's nothing wrong with self.container == [], there is a builtin function for that purpose, so we may as well use it. And while we're at it, it's often nice to use your own internal functions, so behavior is more consistent.
def push(self, item):
self.container.append(item) # appending to the *container*, not the instance itself.
def pop(self):
return self.container.pop() # pop from the container, this was fixed from the old version which was wrong
def size(self):
return len(self.container) # length of the container
s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print s
http://docs.python.org/2/tutorial/datastructures.html # using-lists-as-stacks –
Даже если вашему коду удается превратить ваш объект в список, не означает ли это, что вы потеряете все свои собственные методы? –
Должно быть просто pop() не pop (0). pop (0) делает его очередью. – Aravind