Not my code but I think free to share.

This is intended as beginning point of learning classes or learning data structures.

Read the document
http://mcsp.wartburg.edu/zelle/python/python-first.html

As first exercise, this does not work instead of the while loop in test part:

for i in numbers: print i

Traceback (most recent call last):
File "D:\Python Projects\Tests\stack.py", line 39, in <module>
for i in numbers: print i
TypeError: iteration over non-sequence

Do you find a way to make it work?

Edited 6 Years Ago by pyTony: n/a

class Stack:
    """ 
Here is example of simple Python class from document
http://mcsp.wartburg.edu/zelle/python/python-first.html
which I recommend to read as it helps evangelize virtues of Python

This is intended as start to experiment classes as python has pop() for lists
which is better than fixed stack.

This is not pythonic code, but can you make one class, which is more pythonic,
can be used in for loop etc.
"""

    def __init__(self,size):
        self.data = [None]*size
        self.size = 0

    def push(self,item):
        self.data[self.size] = item
        self.size = self.size + 1    

    def pop(self):
        self.size = self.size - 1
        return self.data[self.size]    

    def is_empty(self):
        return self.size == 0

    def is_full(self):     
        return self.size == len(self.data)

## testing
if __name__=="__main__":
        
    numbers=Stack(100)
    for i in range(10):
        numbers.push(i)

    while not numbers.is_empty(): print numbers.pop()

Specialties:
IT/Science/Contracts/Religious translation/interpreting FIN-ENG-FIN
Python programming

And in that python document is some nice recipe and also a hook to experiment:

The rotate() method provides a way to implement deque slicing and deletion. For example, a pure Python implementation of del d[n] relies on the rotate() method to position elements to be popped:

def delete_nth(d, n):
    d.rotate(-n)
    d.popleft()
    d.rotate(n)

To implement deque slicing, use a similar approach applying rotate() to bring a target element to the left side of the deque. Remove old entries with popleft(), add new entries with extend(), and then reverse the rotation. With minor variations on that approach, it is easy to implement Forth [Edit: /wiki/Forth_%28programming_language%29 ] style stack manipulations such as dup, drop, swap, over, pick, rot, and roll.

Edited 6 Years Ago by pyTony: n/a

Notice that example methods first parameter was not self but d.

You can decide what you call yourself.

Edited 6 Years Ago by pyTony: n/a

Well, if you want to be able to do this: for i in numberstack , you have to write the stack like this:

class Stack:
    """doc omitted"""

    def __init__(self,size):
        self.data = [None]*size
        self.size = 0

    def push(self,item):
        self.data[self.size] = item
        self.size = self.size + 1    

    def pop(self):
        self.size = self.size - 1
        return self.data[self.size]    

    def is_empty(self):
        return self.size == 0

    def is_full(self):     
        return self.size == len(self.data)
    def __iter__(self):
        for i in range(len(self.data)):
            yield self.data[i]

if __name__ == "__main__":
        
    numbers=Stack(100)
    for i in range(100):
        numbers.push(i)

    for x in numbers:
        print x

Edited 6 Years Ago by jcao219: n/a

Ok. I tell you, if the hint was not enough.
You should not use range(len(self.data)) but range(self.size) Here my own code including __iter__

class Stack:
    """ 
Here is example of simple Python class from document
http://mcsp.wartburg.edu/zelle/python/python-first.html
which I recommend to read as it helps evangelize virtues of Python

This is intended as start to experiment classes as python has pop() for lists
which is better than fixed stack.

This is not pythonic code, but can you make one class, which is more pythonic,
can be used in for loop etc.
"""

    def __init__(self,size):
        self.data = [None]*size
        self.size = 0

    def push(self,item):
        self.data[self.size] = item
        self.size = self.size + 1    

    def pop(self):
        self.size = self.size - 1
        return self.data[self.size]    

    def is_empty(self):
        return self.size == 0

    def is_full(self):     
        return self.size == len(self.data)

    def __iter__(self):
        for i in range(self.size):
            yield self.data[i]

## testing
if __name__=="__main__":
        
    numbers=Stack(100)
    for i in range(10):
        numbers.push(i)

    for i in numbers: print i

I ran into one code, which used poster's own stack class and I wanted to prove how well it worked compared to my own code. I thought to not search and replace and recode around the code, but to do enough implementation based on list.

First I tried:

>>> stack = list
>>> stack.push = list.append

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    stack.push = list.append
TypeError: can't set attributes of built-in/extension type 'list'
>>>

No go? No problem, let's inherit:

class stack(list):
    def push(self,x):
        self.append(x)

Let's try to run it. Error code:

expons[expStack.top] += 1
AttributeError: 'stack' object has no attribute 'top'

Ok, it should have attribute for topmost value without removing it. We'll just do one calculated property for it.

@property
    def  top(self):
        return self[-1]

And voila! The program run after simple implementation of prod product of a sequence like sum statement:

from operator import mul
prod = lambda x: reduce(mul, x)
>>> stack([1,2,3,4])
[1, 2, 3, 4]
>>> s = stack([1,2,3,4])
>>> s
[1, 2, 3, 4]
>>> s.pop()
4
>>> s
[1, 2, 3]
>>> s.top
3
>>> type(s)
<class '__main__.stack'>
>>> s
[1, 2, 3]
>>> s.push(4)
>>> s
[1, 2, 3, 4]
>>>

Better name would have been Stack according to Python naming conventions, but the original code used that. Also list built in type deviates from the convention (like other built in types).

Edited 5 Years Ago by pyTony: n/a

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.