Lesson 8 - OOP
Lesson 8 - OOP
(Programming in Python)
LESSON 8: OBJECT ORIENTED PROGRAMMING (OOP)
Learning Outcomes:
At the end of the lesson, students should be able to:
1. Describe the paradigm of Object Oriented Programming (OOP)
2. Describe the “pillars” of OOP
3. Create a class with instance attributes, class attributes, and the __init__()
method.
4. Use a class definition to create class instances to represent objects.
5. Create and implement __init__() with multiple parameters including default
parameter values.
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.
1
OOP makes it possible to create full reusable applications with less code and
shorter development time
2
8.3 Classes and Instances
In a Python program, a class defines a type of object with attributes (fields) and
methods (procedures). A class is a blueprint for creating objects. Individual objects
created of the class type are called instances.
Example 8.1
class polygon:
poly_name = {3:'Triangle', 4:'Quadrilateral',
5:'Pentagon', 6:'Heptagon', 7:'Hexagon',
8:'Octagon',
9:'Nonagon', 10:'Decagon'}
def sum_of_interior_angles(self):
return (2*self.sides - 4)*90
def get_name(self):
name = polygon.poly_name.get(self.sides)
if name == None:
return "Invalid number of sides"
else:
return name
pol1 = polygon(3)
3
print(f"Name of polygon is {pol1.get_name()}")
print(f"Sum of interior angles is
{pol1.sum_of_interior_angles()}")
Sample output
Name of polygon is Triangle
Sum of interior angles is 180
4
Sample implementation