What is object oriented
programming?
Object-oriented programming,
or OOP, is a programming paradigm.
Since the mid 1980’s, it has in fact been the
main programming paradigm. It evolved
out of a need to simplify our dealings with increasingly complicated software (See
footnotes: “How to Think Like a Computer Scientist…”).
Python is “infested by
objects”[1]…
but what are these objects? The main difference between an object vs. simply a piece of information is that an object contains
both data, and the different behaviors that can be associated with that
data. “An object is a software bundle of
related state and behavior.”[2] Strictly
speaking, an object is “an instance of a class,”[3] where
a class is a “blueprint” for that particular type of object. This “blueprint”
includes the following: a class name, which is the name of the type of object;
attributes, which are pieces of meta-data regarding the object; and methods,
which are the operations that can be performed on such an object.
Although we were not aware of
it at the time, what we were doing last term in CSC 108 was procedural programming. This is another
programming paradigm, which makes use, primarily, of writing functions and
calling them, in order to make one’s program run. Object-oriented programming,
on the other hand, makes full use of Python’s abilities as an object-oriented
programming language by creating “objects
which contain both data and functionality together,” [4]
How does it benefit our
understanding and use of Python?
One of the goals of CSC 108
and 148 is to begin to instill in us an aversion to excess and to redundancy.
In this course, we have already learned a few ways of minimizing effort on the
part of the programmer, whilst simultaneously making code easier to understand,
and sometimes, as an added bonus, maximizing efficiency on the part of the
computer. Examples of this include: replacing code with built-ins that do the
same thing (“don’t reinvent the wheel”[5]);
using comprehensions to produce a new list, tuple, dictionary, set, or similar
data structure; and using ternary to evaluate one expression or another based
on a condition.
Like these tools for
efficiency, the approach of object-oriented programming is in keeping with the
first two cardinal virtues of programming: laziness and impatience (the third
and final one being “hubris”). As programmers, ideally, we never want to write
the same code more than once. Emphasizing class definition over function
definition, as one does in object-oriented programming, allows us to “create
relationships between one object and another” by basing a new class on an old
class.”[6]
This makes it possible to build upon and modify work we have already done in
order to fit a new situation (i.e. solve a new problem) without changing, nor
having to repeat the original work. Simplifying and reducing our coding workload
in this way makes it possible to create more complex programs and solve a wider
variety of problems.
[1] CSC148 winter 2014, Introduction to computer science,
week 1, Danny Heap (http://www.cdf.toronto.edu/~heap/148/W14/Lectures/danny/W1/intro.pdf)
[2] The Java Tutorials, What is an Object?
(http://docs.oracle.com/javase/tutorial/java/concepts/)
[3] Introduction to Object
Oriented Programming Concepts (OOP) and More, 4.4. What is an Object? (http://www.codeproject.com/Articles/22769/Introduction-to-Object-Oriented-Programming-Concep#Object)
[4] How to Think Like a Computer Scientist: Learning with
Python 3, 15.1. Object-oriented programming (http://openbookproject.net/thinkcs/python/english3e/classes_and_objects_I.html)
[5] CSC148 winter 2014, abstraction and idiom, week 2,
Danny Heap (http://www.cdf.toronto.edu/~heap/148/W14/Lectures/danny/W2/w2.pdf)
[6] Webopedia, object-oriented programming
(http://www.webopedia.com/TERM/O/object_oriented_programming_OOP.html)