2007. 7. 11. 21:10
Computation/Language
Using Python for CGI programming
Author: Guido van Rossum
Email: guido@python.org
Home Page: http://www.python.org/~guido/
Subclasses
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)
"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
- 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
'Computation > Language' 카테고리의 다른 글
numeric library (0) | 2008.03.18 |
---|---|
Python tutorials (0) | 2008.02.24 |
classes in Python (0) | 2007.07.11 |
Python (0) | 2007.06.26 |
features of object-oriented programming (0) | 2007.06.20 |