LESSON 13

Object-Oriented Programming

Learning objective: Model things with classes and objects.

Understand

Classes let you create your own data types.

A class is a blueprint; an object is an instance of it. Classes bundle data (attributes) and behaviour (methods). The __init__ method sets up each new object. OOP helps organize larger programs.

Analogy: A class is a cookie cutter; each object is a cookie made from it.

See It in Action

A simple class:

class Dog:
    def __init__(self, name):
        self.name = name
    def bark(self):
        print(self.name + " says woof")

Dog("Rex").bark()
How it works: __init__ stores the name on each object; bark is a method that uses it. Dog('Rex') creates an object and calls its method.

Try It Yourself

  1. Define a class with an __init__.
  2. Create an object from it.
  3. Add and call a method.

Quick Quiz

What is an object in OOP?

Challenge

Create a class to represent something real and give it one method.

Success condition: Your object is created from a class and its method works.