I’ve been learning and coding with Python for roughly four years now. But one of its greatest features, object oriented programming, is something that I have never really understood. Starting in March of this year, I taught
a Python class at the University of San Francisco (USF) to business students. I did not talk about OOP during this class but it got me thinking, if I am to be an ‘expert’ of this language and teach it to students, then maybe
OOP is something that I should know like the back of my hand. So yesterday, Monday May 14th, I whipped out the text book being used for the class, and started to plough through the OOP chapters. The textbook is Starting Out
With Python by Tony Gaddis. Turns out Gaddis is a master when it comes to writing textbooks. Anyway, here is an overview of my learnings from OOP in Starting Out With Python.
Up until now, I have been programming procedurally, meaning a program was made of one or more procedures. Whereas procedural programming is centered on creating procedures (functions), OOP is centered on creating objects.
Objects is a software entity that contains both data and procedures. the data contained in an object is known as the object’s data attributes. This is simply variables that reference data. The procedures that an object performs are
known as methods. An object’s methods are functions that perform operations on the object’s data attributues. Conceptually speaking, the object is a self-contained unit that consists of data attributes (data) and methods (functions)
that operate on the data attributes.
OOP addresses the problem of cose and data separtion through excapsulation, the combining of data and code into a single object. Only the object’s methods may directly access and make changes to the object’s data attributes.
A class is code that specifies the data attributes and methods for a particular type of object. Think of a class as a blueprint from which objects may be created, much like a blueprint for a house. The blueprint itself is not
a house, but is a detailed descripion of a house. When used to build an actual house, I could say I am building an instance of the house described by the blueprint. I can build multiple identical houses from the same blueprint.
Each house is a separate instance of the house described by the blueprint. In summary, the blueprint is a class and the house(s) is/are the object(s)
A simple example I’ve created of this is with a class named Fish. This class specifies characteristics that are common to all types of fish and used to create objects. Next, I create an object names salmon, which is an instance
of the Fish class. The salmon object is an entity that occupies computer memory and stores data about a salmon. It has the data attributes and methods specified by the Fish class. I then create another object named halibut.
Again, the halibut object is an instance of the Fish class. It has its own memory and stores data about a halibut. Although these are two different objects, they were both created from the same Fish class and therefore
means that each of the objects has the data attributes and methods described by the Fish class.
To be continued ….