Week 6
Object-Oriented Programming Concepts
(Please grade this)
Concepts are of main
importance in object-oriented programming. They are the basis on which we can
construct our knowledge. During the last 6 weeks we have been talking about different
kinds of Abstract Data types, recursion and trees. I would like to briefly summarize
some of the key concepts about these topics here:
1.
Abstract Data types
Abstract
Data Types, as its name suggests, has at its core abstracting the information
and operations by hiding the details. Some examples of abstract data types are
stacks, csv files, queues and dictionaries. Each of these examples include data
that can be accessed in different ways but they all keep their abstract style.
Stacks (First in, last out)
·
The first
item added is the last one to be removed. Just like a pile of books that depend
on the one below to avoid falling.
·
Some of the
operations and attributes are:
Push: Add an item
on to the top of the stack
Pop: Removed an
item from the top of the stack.
Size: The number of elements that the stack contains.
Queues (First in, first out)
·
The first
item added is the last one to be removed. Just like people waiting in line.
·
Some of the
operations and attributes are:
Push: Add an item
at the end of the queue
Pop: Removed an
item from the beginning of the queue.
Size: The number of elements that the queue contains.
Stacks and queues are very useful techniques that allow us to store
information. The implementation is very similar, but it is necessary to
understand how items are added and removed.
Classes
Classes and subclasses
also play a role in Object-Oriented Programming in Python. They are just a way to
represent information. They include different methods and attributes.
Special Methods
Python comes with these
methods for its own classes, but we need to create our own methods for our
classes. A clear example of the importance of these methods lies on the method
__eq__ without whom we would be comparing just memory addresses. Some examples of methods are the following:
__init__ Used to initialize variables that are given as parameters
when creating an instance of the class.
__str__ Used to give a string version of the class.
__repr__ Used to give a string representation of the class
__eq__ Used to compare attributes among instances of a class
Attributes
Inheritance
In python we have
classes and subclasses. Subclasses usually inherit attributes from their parent
classes. It is usually done in the following way:
class InheritsFrom(ParentClass):
The methods of a
subclass can inherit some attributes, but also add more information and
variables. Example:
def __init__(self,
subclass_parameter):
ParentClass.__init__(self,
class_parameters)
self.new_variable = subclass_parameter
Not implemented errors
We can raise Not Implemented
Errors when a method is going to be implemented in a subclass. Example:
raise NotImplementedError(‘need
subclass’)
2.
Recursion
Recursion
is the process of calling a function within the same function. It is a powerful
mean that allows us to repeat a process a considerable number of times. This
process combined with list comprehension gives us the ability to create short
and neat code. An example of the use of recursion is the following:
def count(L):
if not(isinstance(L, list)):
return
1
else:
return sum([count(x) for
x in L])
Tracing code comes along with recursion and base cases are needed to
find the right solution.
3. Trees
Trees
include more definitions than the previous topics. Some of these concepts are
here:
Tree: A set of nodes.
Nodes: Part of a three that may contain values or labels and are connected with
directed edges.
Root: Special node that has no parents.
Path: A sequence of k nodes connected by edges. There is a unique path.
Length of a path: The number of edges on it.
Leaf: A node that has no children
Internal node: Nodes that have one or more children. In other
words, nodes that are not leafs.
Subtree: Tree formed by any tree node. A tree is made of subtrees,
which include their descendants and the paths leading to them.
Height: one plus the maximum path length in a three. The one is added to account
for the imaginary tree in space.
Depth: Height of the tree minus the height of a node.
Arity, branching factor: Maximum number of children for any node. This is basically a number that guides us to create the tree.
Tree example:
class Tree:
def __init__(self, value=None, children=None):
self.value = value
self.children = children.copy() if children else []
In the example above, we have created a class tree that will allow us to create trees given its value and children.