File: class/Workbook/Examples/Lecture15/oopstack.py

import stacktype                               # get C type
class Stack:
    def __init__(self, start=None):            # wrap | make
        self._base = start or stacktype.Stack()
    def __getattr__(self, name):
        return getattr(self._base, name)       # attributes
    def __cmp__(self, other):
        return cmp(self._base, other)
    def __repr__(self):                        # 'print'
        print self._base,; return ''
    def __add__(self, other):                  # operators
        return Stack(self._base + other._base)
    def __mul__(self, n): 
        return Stack(self._base * n)           # new Stack
    def __getitem__(self, i): 
        return self._base[i]                   # [i],in,for
    def __len__(self):
        return len(self._base)



[Home page] Books Code Blog Python Author Train Find ©M.Lutz