#!/usr/bin/env python
class Stack:
# This is simply an abstract class emulating a list.
# Just with another name :-P
internal_list = []
def __init__(self,value=None):
"Pass some initial values to the stack."
if value:
self.push(value)
def push(self,value):
"""Put new elements on the stack. If the argument is a list,
the elements of the list will be appended."""
if type(value) == type([]):
for v in value:
try:
self.internal_list.append(v)
except:
return False
return True
else:
try:
self.internal_list.append(value)
except:
return False
else:
return True
def pop(self):
if len(self.internal_list) == 0: raise EOFError, "Stack is already empty"
try:
return self.internal_list.pop()
except:
return False
# Now, let's try it!
if __name__ == '__main__':
stack = Stack(['Haus','Welt','Physik'])
stack.push('Hallo Welt')
stack.push(['Hallo','Hello','World'])
while True:
s = stack.pop()
if not s:
break
print s