← Lesson
BitWithBite
Python · Quick Reference

Lesson 11 — OOP & Classes Cheat Sheet

Python
In one line: OOP is a programming paradigm that organises code around objects — bundles of related data (attributes) and behaviour (methods). A class is the blueprint; an instance is the act...

Key Ideas

1What is Object-Oriented Programming?. OOP is a programming paradigm that organises code around objects — bundles of related data (attributes) and behaviour (methods). A class is the blueprint; an instance ...
2Defining a Class. Use the class keyword. The __init__ method (the constructor) runs automatically every time you create an instance. self refers to the instance being created — it must ...
3Dunder Methods — Making Objects Pythonic. Dunder methods (double-underscore methods, also called magic methods) let your objects behave like built-in Python types. Define __str__ to control how an object looks...
4Class Methods & Static Methods. Not all methods need access to a specific instance. Python gives you two decorators: @classmethod for methods that work with the class itself (not an instance), and @s...
5Properties — Controlled Attribute Access. The @property decorator lets you access a method like an attribute. This is Python's way of adding validation or computation to attribute access without breaking the i...

Code Examples

class Student: """Represents a student with name, age, and grades.""" # Class attribute — shared by ALL instances school = "BitWithBite Academy" def __init__(self, name, age, gpa=0.0): # Instance attributes — unique per object ...
class ShoppingCart: def __init__(self, owner): self.owner = owner self.items = [] def add(self, item, price): self.items.append((item, price)) def total(self): return sum(p for _, p in self.items) # __str__...
class BankAccount: _total_accounts = 0 # class attribute — tracked globally INTEREST_RATE = 0.05 # constant — class attribute def __init__(self, owner, balance=0): self.owner = owner self.balance = balance BankAcc...