As the blogger at http://lhzuigao.blogspot.ca/ mentioned,
concepts of object oriented programming (OOP) are very different from those we
learned in CSC108 where our main focus was on detailed mechanics and rules of
python coding. One of the most significant differences I have realized thus far
in the course is that my coding looks simpler, cleaner, and yet more
sophisticated with OOP approach. To this end, I am going to lay out a summary
of OOP concepts that were discussed in this course.
1. Abstract Data Types (ADTs)
Objects can contain information and
behaviors. In CSC108, we learned how to store information that an object
represents in different types of structures, such as dictionary and list, and
to implement functions that uses these structures. We now move away from
details of implementation and focus on Abstract Data Types which combines
information and behavior in one data structure. ADTs specify a set of behaviors
(or operations) and their purposes. As shown below with the example of Stack, details
of how we should implement operations or store information are not considered
here and remain “abstract”.
a) Stack
Stack is an example of ADT that stores
items in any structure we want to use (because it is “abstract” data type). Possible
candidates for storage include lists or dictionaries. Operations that can be
used on a stack ADT are:
i) pop: remove the last item of a stack
ii) push: add a new item onto the top of
the stack
iii) is_empty: tells us whether a stack is
empty
b) Classes
In python, data of similar types are
specified by classes. Once I know what to expect for my ADT, I can start
implementing it by defining a new class.
c) Methods
Functions that are defined in a class are
called method. Every class, newly defined or existing, inherits special methods
from a common ancestor, or an object. These special methods include the followings:
__init__: This method is called when an
instance of a class is created. Instance variables are created if the class takes
arguments
__str__: This method is called to print an
instance of a class.
__repr__: This method returns a string that
can construct an object with the same value.
__eq__: This method compares two instances
and returns True if they are the same otherwise returns False.
Important note to make here is that a special
method defined in a given class may act differently from the same special method
defined in another class or a similar module-level function. For instance, we
might want to add an extra argument to the method __str__ in our class even
though class str at module-level does not take an argument when instantiated.
d) Inheritance and Exceptions
As mentioned above, classes inherit special
methods from a common ancestor. Any classes may have subclass which inherits
attributes and methods defined in its superclass. These methods will behave the
same way unless we override them by replacing with our own methods. When we
want a method to work in specific cases rather than in general cases, we raise ‘NotImplementedError’
in superclass and define it in the subclass. Whenever a method is called to an
instance of a subclass, Python will check if that method is implemented in the
subclass first. If not, Python will use the method defined in the superclass.
2. Recursion
Sometimes we see a function being called
within it. Such functions are called recursive function. For the problems that
are solved with recursion, we can derive same results by using iterations such
as for- and while-loops. However, with recursion, amount of repetitive codes
can be reduced significantly.
3. Trees and Binary Search Trees
A Tree
is a set of nodes that may contain
values. An edge connects a pair of
nodes, one node being a parent and
the other being a child, such that
there are no edges between two parents or two children. There exists one node
at the top of the tree called a root,
and every node has exactly one parent except the root of a tree. A path of a tree refers to a sequence of nodes
and the edges between them and the length
of a path is determined by counting the number of edges on it. A node with
at least one child is called internal
node while a node with no children is called leaf. Within a tree, subtrees
can be formed by any node and its children and edges connecting them. Height of a tree refers to the number
of nodes in the longest path of a tree and is determined by adding length of
maximum path and 1. Depth of a node
is determined by subtracting height of the tree rooted at that node from the
height of the whole tree. Finally, maximum number of children that a node can
have is defined as arity or branching
factor.
An example of a tree we learned in class is
a binary tree. Unlike a tree where each node was allowed to have unlimited
numbers of children, each node in binary tree can have up to two children (left
and right child).
No comments:
Post a Comment