Computation/Language

Using Python for CGI programming

maetel 2007. 7. 11. 21:10
Using Python for CGI programming

Author: Guido van Rossum
Email: guido@python.org
Home Page: http://www.python.org/~guido/


Subclasses
class Stack:
    "A well-known data structure..."
    def __init__(self):
        self.items = []
    def push(self, x):
        self.items.append(x)
    def pop(self):
        x = self.items[-1]
        del self.items[-1]
        return x
    def empty(self):
        return len(self.items) == 0
   
#x = Stack()
#x.empty()
#x.push(1)
#x.empty()
#x.push("hello")
#x.pop()
#print x.items
#x.push("hello")
#print x.items
   
class FancyStack(Stack):
    "stack with added ability to inspect inferior stack items"
   
    def peek(self, n):
        "peek(0) returns top; peek(-1) returns item below that; etc."
        size = len(self.items)
        assert 0 <= n < size
        return self.items[size-1-n]
       

class LimitedStack(FancyStack):
    "fancy stack with limit on stack size"
   
    def __init__(self, limit):
        self.limit = limit
        FancyStack.__init__(self)
       
    def push(self, x):
        assert len(self.items) < self.limit
        FancyStack.push(self, x)

> Instance variable rules
- Use via instace (self.x), search order: (1) instance, (2) class, (3) base classes
- assignment via instace (self.x = ...) always makes an instacne variable
- Class variables "default" for instance variables