Using stack in python gives NoneType error
<p>I am trying to push an item on to stack in Python. Below is the code for trying to push the item :</p>

<pre><code>class Search
def generalGraphSearch(problem,fringe):
closed=set()
    #If no nodes
    if problem.isGoalState(problem.getStartState()):
        return problem.getStartState()
    #Create object of Stack class
    stackOb = util.Stack()
    """Push the starting node into the stack. The parameter is the state"""
    stackOb.push(problem.getStartState())
    print stackOb.push(problem.getStartState())

The stack implementation is as below :
class Stack:
    "A container with a last-in-first-out (LIFO) queuing policy."
    def __init__(self):
        self.list = []

    def push(self,item):
        "Push 'item' onto the stack"
        self.list.append(item)
</code></pre>

<p>The print statement in the Search class gives type as none</p>

<p>Any suggestions how to overcome this problem ?
Thanks</p>

<p>You are trying to print the result of <code>push()</code> method call, but the method <em>does not return anything</em> - that's why you see <code>None</code> printed.</p>

<p>Instead, you meant to either explore the contents of the <code>list</code> attribute:</p>

<pre><code>stackOb.push(problem.getStartState())
print(stackOb.list)
</code></pre>

<p>or, implement and use the <code>pop()</code> method to get the element from the top of the stack:</p>

<pre><code>class Stack:
    # ...

    def pop(self):
        return self.list.pop()
</code></pre>

<p>You can also have a <code>peek()</code> method that would simply return the top element from the stack without removing it:</p>

<pre><code>class Stack:
    # ...

    def peek(self):
        return self.list[-1]
</code></pre>

python python-2.7 stack