3. Object-oriented programming

What is Object-oriented programming?

  • A flexible, powerful paradigm where classes represent and define concepts, while objects are instances of classes.

  • The attributes are the characteristics associated to a type.

    • For example, a file has many attributes such as its:

      • Name

      • Size

      • Date

      • Permission, etc.

  • The methods are the functions associated to a type.

Classes and Objects in Python

Almost everything in Python is an object.

>>> type(0)
<class 'int'>
>>> type('hello')
<class 'str'>

Our Apple class:

>>> class Apple:
... color = ""
... flavor = ""
>>> type(jonagold)
<class '__main__.Apple'>
>>> jonagold = Apple()
>>> jonagold.color = "red"
>>> jonagold.flavor = "sweet"
>>> print(jonagold.color)
>>> print(jonagold.flavor)

Instance Methods

Example 1: A boring method that does the same for all piglets:-)

>>> class Piglet:
... def speak(self):
... print("oink oink")
>>> hamelt1 = Piglet()
>>> hamelt1.speak()
oink oink
>>> hamelt2 = Piglet()
>>> hamelt2.speak()
oink oink
  • self represents the current instance of the class.

Example 2: A little more fun class where Pigs can even speak:-)

>>> class Piglet:
... name = "Piglet" # Instance Variable
... def speak(self):
... print("Oink! I'm {}! Oink!".format(self.name))
>>> hamlet = Piglet()
>>> hamlet.name = "Hamlet"
>>> hamlet.speak()
Oink! I'm Hamlet! Oink!
  • Variables that have different values for different instance of the same class are called instance variables. Just like the name variable in the above example.

Constructors and Other Special Methods

The constructor of a class is the method that's called when you call the name of the class. It's always named init.

>>> class Apple:
... def __init__(self, color, flavor):
... self.color = color
... self.flavor = flavor
>>> jonagold = Apple("red", "sweet")
>>> print(jonagold.color)

Inheritance - (is-a relationship)

# Base class that has all the properties
class Fruit:
def __init__(self, color, flavor):
self.color = color
self.flavor = flavor
class Apple(Fruit): # Apple class is inheriting the properties of Fruit class
class Grape(Fruit): # Grape class is also inheriting the properties of Fruit class
apple1 = Apple("green", "tart")
print(apple1.color) # green
grape1 = Grape("purple", "sweet")
print(grape1.color) # purple
  • Apple is a Fruit.

  • Grape is a Fruit.

class Animal:
sound = ""
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.sound} I'm {self.name}! {self.sound}")
class Piglet(Animal):
sound = "Oink!"
hamlet = Piglet("Hamlet")
hamlet.speak() # Oink! I'm Hamlet! Oink!
class Cow(Animal):
sound = "Moooo"
milky = Cow("Milky White")
milky.speak() # Moooo I'm Milky White! Moooo

Composition (Has-a Relationship)

Composition is a concept that models a has a relationship. It enables creating complex types by combining objects of other types. This means that a class Composite can contain an object of another class Component.

You can have a situation where two different classes are related, but there is no inheritance going on. This is referred to as composition -- where one class makes use of code contained in another class.

  • Dog -> bark + eat

  • CleanBot -> Move + Clean

  • SuperBot -> Move + Clean + Bark + Play Games


Used to organize functions, classes, and other data together in a structured way.

For example, random, datatime

Small Project Idea:

Create a program that has an employee class, so your manager can keep track of all the assigned project with its status (done, pending, ongoing). Save the work in a CSV format.

  • First name, Last Name, Email

  • Group that the Project has been assigned to.

  • Project Status