1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/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