Tutorial 6: Review
Python Programming, 3/e 1
Object-oriented design (OOD)
OOD is the process of developing a set
of classes to solve a problem. It is
similar to top-down design in that the
goal is to develop a set of black boxes
and associated interfaces. Where top-
down design looks for functions, OOD
looks for objects.
Python Programming, 3/e 2
Guidelines to do OOD
Look for object candidates.
Identify instance variables.
Think about interfaces.
Refine nontrivial methods.
Design iteratively.
Try out alternatives.
Keep it simple.
Python Programming, 3/e 3
Principles of OOD
Encapsulation Separating the implementation details
of an object from how the object is used. This
allows for modular design of complex programs.
Polymorphism Different classes may implement
methods with the same signature. This makes
programs more flexible, allowing a single line of
code to call different methods in different situations.
Inheritance A new class can be derived from an
existing class. This supports sharing of methods
among classes and code reuse.
Python Programming, 3/e 4
How to create a class
class Dog:
def __init__(self):
pass
This code defines a class called "Dog" in Python. The
class has a constructor method called "init" which
takes in the "self" parameter. The "pass" keyword is
used to indicate that the method does not have any
code to execute. This constructor method is called
when an object of the "Dog" class is created.
Python Programming, 3/e 5
Adding attributes to a class
class Dog:
def __init__(self, name, age):
[Link] = name
[Link] = age
Python Programming, 3/e 6
Instantiating objects
To instantiate an object, type the class
name, followed by two brackets. You
can assign this to a variable to keep
track of the object.
ozzy = Dog()
print(ozzy)
<__main__.Dog object at 0x111f47278>
Python Programming, 3/e 7
Python Programming, 3/e 8
Python Programming, 3/e 9
Python Programming, 3/e 10
Python Programming, 3/e 11
Key Points to Remember
Object-Oriented Programming makes the
program easy to understand as well as
efficient.
Since the class is sharable, the code can be
reused.
Data is safe and secure with data abstraction.
Polymorphism allows the same interface for
different objects, so programmers can write
efficient code.
Python Programming, 3/e 12
Python Programming, 3/e 13
Python Programming, 3/e 14
Python Programming, 3/e 15
Python Programming, 3/e 16