Day7 stack

class Stack(object):
    """
    Stack object
    """
    def __init__(self, limit=10):
        """
        :param limit: the stack size
        """
        self.stack = []
        self.limit = limit

    def __str__(self):
        return ' '.join([str(i) for i in self.stack])

    def push(self, data):
        """
        pushes an item into the stack
        returns -1 if the stack is empty
        """
        if len(self.stack) >= self.limit:
            # indicates stack overflow
            return -1       
        else:
            self.stack.append(data)

    def pop(self):
        """
        pops the topmost item from the stack
        returns -1 if the stack is empty
        """
        if len(self.stack) <= 0:
            # indicates stack underflow
            return -1       
        else:
            return self.stack.pop()

    def peek(self):
        """
        returns the topmost element of the stack
        returns -1 if the stack is empty
        """
        if len(self.stack) <= 0:
            # stack underflow
            return -1       
        else:
            return self.stack[len(self.stack) - 1]

    def is_empty(self):
        """
        checks if the stack is empty
        returns boolean value, True or False
        """
        return self.size() == 0

    def size(self):
        """
        returns the current size of the stack
        """
        return len(self.stack)

    @staticmethod
    def get_code():
        """
        returns the code for current class
        """
        return inspect.getsource(Stack)


・__init__(self, limit=10)........上限値らしい。

・self.size().........おそらくデータサイズのこと。

・@staticmethod..........デコレータ だって。インスタンス化しなくても呼び出し可能な関数のことらしいがよくわからんが、やりながら覚えよう。
【Python入門】デコレータの使い方をわかりやすく解説! | 侍エンジニアブログ

・inspect.getsource(Stack)...................活動中のオブジェクトから情報を取得する関数を定義している(inspect)。オブジェクトのソースコードを返す(.getsource)


明日もこちらをやってみます。ありがとうございます!
pygorithm/stack.py at master · OmkarPathak/pygorithm · GitHub