Sunday, 15 February 2015

review of object oriented design

summary of O-O design :
(1).ID and memory address : .Every value in a Python program is an object and has a (virtual) memory address.You can check this using id, or is (try typing these in a Python interpreter)

(2)what is self in python :
(quoted from the required reading )
Now that we're doing things properly, our method has a docstring that includes a type contract and a brief description that mentions every parameter. If you're wondering why the type contract returns NoneType, it helps to know that this method takes a vanilla new object and adds attributes to it. Also, there's nothing special about the name self for the parameter. We could have used me, you, or ego and Python would have understood this parameter as referring to the new instance of Point in the body of the init method. 

(3)__eq__ , __repr__, __str__ , __init__(several method used in Class)
__eq__: return a boolean 
__repr__:return a string
__str__:return a str
__init__:initialize the Class

(4)ADT (abstract data types)
1.Stack ADT: A stack contains items of various sorts. New items are pushed on to the top of the stack, items may only be removed from the top of the stack. (We say that an item is “popped” from the stack.) It’s a mistake to try to remove an item from an empty stack. We can tell how big a stack is, and what the top item is.

stack function:
a)pop:Remove and return the top item from self.
b)push:Place object o on top of Stack self.
c)is_empty:Return whether the self is empty.

2. Tree:Represent a Bare-bones Tree ADT

Tree function
list_all(t)Return a list of values in t.
list_leaves(t) Return a list of values in leaves of t.
list_interior(t):Return a list of values in interior nodes of t.
list_if(t, p): Return a list of values in Tree t that satisfy p(value)
list_to_depth(t, n):Return a list of values in t from nodes with paths no longer
    than n from root
gather_lists(L): Concatenate all the sublists of L and return the result.
descendent_from_list(t, L, arity):Populate t's descendent from L, filling them
in in level order, with up to arity children per node.
    Then return t.

(5)inheritance: 
upperclass : a general class for a kind of things , we will return NoneType in some method 
subclass: a specific class , we can override upper class, inherit the things we need and complement the things we need 

(6)recursion : call a function over and over again 


example:
(1)
>>> w1 = "words"
>>> w2 = "swords"[1:]
>>> w1 == w2
>>> w1 is w2
False







(6)http://www.cdf.toronto.edu/~csc148h/winter/Labs/lab03/handout.pdf

No comments:

Post a Comment