Object-Oriented Programming (OOP) for Codedexters
Object-Oriented Programming (OOP) for Codedexters
An object contains data, like the raw or preprocessed materials at each step on an
assembly line. In addition, the object contains behavior, like the action that each
assembly line component performs.
For example, an object could represent a person with properties like a name, age,
and address and behaviors such as walking, talking, breathing, and running. Or it
could represent an email with properties like a recipient list, subject, and body and
behaviors like adding attachments and sending.
Python
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
But what does all of that mean? And why do you even need classes in the first
place? Take a step back and consider using built-in, primitive data structures as an
alternative.
For example, you might want to track employees in an organization. You need to
store some basic information about each employee, such as their name, age,
position, and the year they started working.
Python
kirk = ["James Kirk", 34, "Captain", 2265]
spock = ["Spock", 35, "Science Officer", 2254]
mccoy = ["Leonard McCoy", "Chief Medical Officer", 2266]
There are a number of issues with this approach.
First, it can make larger code files more difficult to manage. If you
reference kirk[0] several lines away from where you declared the kirk list, will
you remember that the element with index 0 is the employee’s name?
Second, it can introduce errors if employees don’t have the same number of
elements in their respective lists. In the mccoy list above, the age is missing,
so mccoy will return "Chief Medical Officer" instead of Dr. McCoy’s age.
A great way to make this type of code more manageable and more maintainable is
to use classes.
Classes vs Instances
Classes allow you to create user-defined data structures. Classes define functions
called methods, which identify the behaviors and actions that an object created
from the class can perform with its data.
In this tutorial, you’ll create a Dog class that stores some information about the
characteristics and behaviors that an individual dog can have.
A class is a blueprint for how to define something. It doesn’t actually contain any
data. The Dog class specifies that a name and an age are necessary for defining a
dog, but it doesn’t contain the name or age of any specific dog.
While the class is the blueprint, an instance is an object that’s built from a class
and contains real data. An instance of the Dog class is not a blueprint anymore. It’s
an actual dog with a name, like Miles, who’s four years old.
Put another way, a class is like a form or questionnaire. An instance is like a form
that you’ve filled out with information. Just like many people can fill out the same
form with their own unique information, you can create many instances from a
single class.
Class Definition
You start all class definitions with the class keyword, then add the name of the
class and a colon. Python will consider any code that you indent below the class
definition as part of the class’s body.
Python
# dog.py
class Dog:
pass
The body of the Dog class consists of a single statement: the pass keyword. Python
programmers often use pass as a placeholder indicating where code will eventually
go. It allows you to run this code without Python throwing an error.
The Dog class isn’t very interesting right now, so you’ll spruce it up a bit by defining
some properties that all Dog objects should have. There are several properties that
you can choose from, including name, age, coat color, and breed. To keep the
example small in scope, you’ll just use name and age.
You define the properties that all Dog objects must have in a method
called .__init__(). Every time you create a new Dog object, .__init__() sets the
initial state of the object by assigning the values of the object’s properties. That
is, .__init__() initializes each new instance of the class.
You can give .__init__() any number of parameters, but the first parameter will
always be a variable called self. When you create a new class instance, then
Python automatically passes the instance to the self parameter in .__init__() so
that Python can define the new attributes on the object.
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age