ALL MODULESmodule_09.py

OOP Part 1 — Classes, Objects & __init__

~60 minTheoryLive CodingChallengeQuiz
theory.md

9. OOP Part 1 — Classes, Objects & __init__

9.1Blueprints & Objects (Classes)

Think of an architectural blueprint vs a physical house built from that blueprint. One blueprint can build 100 identical houses!

SHOW:

PYTHON
WHAT HAPPENED HERE
We defined a class named Student using class. Then we created two independent student objects from that single blueprint.
THE TECHNICAL NAME
The blueprint is a class. The individual items created from it are objects (or instances).
TAKEAWAY
A class is a blueprint; objects are created from that blueprint.

9.2Initializing Object Attributes (__init__ & self)

Think of unboxing a new smartphone — during initial setup, you enter your name and phone owner settings.

SHOW:

PYTHON
WHAT HAPPENED HERE
__init__ ran automatically when Dog("Buddy", ...) was called. self points to the new dog object being set up.
THE TECHNICAL NAME
The __init__ method is the constructor. The self parameter refers to the current instance.
TAKEAWAY
Use def __init__(self, ...): to set initial properties on new objects.

9.3Adding Actions (Instance Methods)

Think of a bank account object — it has properties (balance) and actions you can perform on it (deposit, withdraw).

SHOW:

PYTHON
WHAT HAPPENED HERE
deposit is a method attached to BankAccount. It accessed and modified self.balance for that specific account.
THE TECHNICAL NAME
Functions inside a class are instance methods. Data variables attached to objects are attributes.
TAKEAWAY
Methods are functions attached to objects that access properties via self.

9.4Class vs Instance Attributes

Think of house rules (everyone living in the house shares the same address) vs individual bedrooms (each person has their own private room layout).

SHOW:

PYTHON
WHAT HAPPENED HERE
species is defined at class level and shared by all users. username is unique to each individual User instance.
THE TECHNICAL NAME
Shared variables are class attributes. Object-specific variables are instance attributes.
TAKEAWAY
Class attributes are shared by all instances; instance attributes belong to one object.
GOTCHA 1

Forgetting the self parameter in instance method definitions (raises TypeError when called).

GOTCHA 2

Confusing class attributes (shared by all objects) with instance attributes (defined in __init__).

GOTCHA 3

Using mutable class attributes (like list = []) which end up shared across every instance.

WHERE THIS SHOWS UP IN REAL CODE

Web frameworks like Django and FastAPI represent database records as Python classes (e.g., User, Order, Product).

TRY THIS YOURSELF

Define class Car with __init__(self, brand) and instantiate c = Car('Toyota').

live_coding — E-Commerce Product & Inventory System

Notice how buy() checks stock and updates self.stock in-place.

Python not loaded yet.
demo — Demonstrate class creation, __init__ constructor execution, self parameter, and instance methods.

Demonstrate class creation, __init__ constructor execution, self parameter, and instance methods.

challenge_1

Warm-up 1: Create a Person class with name and age.

Steps:

  1. Define class Person with __init__(self, name, age).
  2. Instantiate p = Person("Ada", 30).
  3. Print Ada is 30 years old..
Python not loaded yet.
challenge_2

Warm-up 2: Add a greeting method to a User class.

Steps:

  1. Class User with __init__(self, username).
  2. Method greet(self) returning Hello, <username>!.
  3. Instantiate u = User("DevGuy") and print u.greet().
Python not loaded yet.
challenge_3

Warm-up 3: Rectangle class with area calculation.

Steps:

  1. Class Rectangle(width, height).
  2. Method area(self) returning width * height.
  3. Instantiate r = Rectangle(5, 4) and print Area: 20.
Python not loaded yet.
challenge_4

Apply-it 1: Counter class with increment and reset.

Steps:

  1. Class Counter starting self.value = 0.
  2. Method increment(self) adding 1.
  3. Method reset(self) setting value back to 0.
  4. Increment twice, print Count: 2.
Python not loaded yet.
challenge_5

Apply-it 2: Circle class with perimeter calculation.

Steps:

  1. Class Circle(radius).
  2. Method perimeter(self) returning 2 * 3.14 * self.radius.
  3. Instantiate c = Circle(10) and print Perimeter: 62.8.
Python not loaded yet.
challenge_6

Apply-it 3: Track shared class attribute vs instance attribute.

Steps:

  1. Class Employee with class attribute company = "TechCorp".
  2. Instance attribute name.
  3. Instantiate e1 = Employee("Sam") and e2 = Employee("Kim").
  4. Print Sam works at TechCorp, Kim works at TechCorp.
Python not loaded yet.
challenge_7

Stretch Challenge: Explain Classes & Build Shopping Basket.

Steps:

  1. Print explanation: A class is a blueprint; objects are instances created from it..
  2. Class Basket with self.items = [].
  3. Methods add_item(self, item) and get_items(self).
  4. Add "Apple" and "Banana", print Items: ['Apple', 'Banana'].
Python not loaded yet.
quiz — 9 questions
30s question timers are paused. Start individually or all together.
# question 1

What is a class in Object-Oriented Programming?

# question 2

What is the role of the `__init__` method in Python?

# question 3

What does the `self` parameter represent inside instance methods?

# question 4

What is an attribute in Python OOP?

# question 5

What is the difference between class attributes and instance attributes?

# question 6

How do you instantiate an object `car1` from a class `Car`?

# question 7

Why must `self` be the first parameter in instance method definitions?

# question 8

What happens if you define `class User: items = []` at class level and append items?

# question 9

Which keyword creates a new class in Python?

discussion

# comments · oop-basics

  • @priya · 2h ago

    The typewriter hero is such a nice touch.

    • @alex · 1h ago

      Totally agree! Made it super engaging.

  • @sam · 1d ago

    Finally a course where I can run code without leaving the page.