Python · Level 1 OOP

OOP & Classes

Model your program around objects that bundle state and behaviour into reusable, modular classes.

  • class & __init__ constructor
  • Instance methods & self
  • Encapsulation
  • Inheritance basics

A class is a blueprint for objects. __init__ runs when you create an instance; self refers to that instance.

class Dog:
    def __init__(self, name, breed):
        self.name  = name
        self.breed = breed

    def bark(self):
        return f"{self.name} says Woof!"

dog = Dog("Rex", "Labrador")
print(dog.bark())  # -> Rex says Woof!

Design a BankAccount class. deposit(amount) and withdraw(amount) should update and return the new balance.

solution.py
Test Suites Pending
Stdout