01 / 20

Defining Classes
in Python

Learn how to create blueprints for objects using Python classes, define attributes and methods, and instantiate your own custom types.

Python

What Is a Class?

A class is a blueprint or template that defines what data an object holds and what actions it can perform. Think of it like an architectural plan: the plan itself is not a house, but you can build many houses from the same plan.

Classes vs Instances

Class: Dog name breed bark() BLUEPRINT Instance: my_dog name="Rex", breed="Lab" Instance: your_dog name="Milo", breed="Pug" Instance: stray name="Buddy", breed="Mix"

Why Use Classes?

Organization
Group related data and functions together into a single, logical unit.
Reusability
Write the blueprint once, create unlimited instances with different data.
Modeling
Represent real-world entities: students, bank accounts, game characters.

The class Keyword

class ClassName: # class body goes here pass
1
class keyword: tells Python you are defining a new type.
2
ClassName: by convention, uses PascalCase (each word capitalized).
3
Colon + indented body: everything indented under the class statement belongs to it.

The __init__ Method

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

Understanding self

When you call: rex = Dog("Rex", "Lab") Python creates the object Python passes: self = rex Automatically, behind the scenes Result rex.name = "Rex" rex.breed = "Lab"

Class vs Instance Attributes

Class Attribute Instance Attribute
Defined where? Directly in the class body Inside __init__ with self
Shared? Yes: all instances share it No: unique per instance
Example species = "Canine" self.name = name
Changed via ClassName.attr instance.attr

Defining Methods

class Dog: def __init__(self, name, breed): self.name = name self.breed = breed def bark(self): return f"{self.name} says Woof!"

How to Define and Use a Class

1
Write the class keyword followed by PascalCase name and a colon.
2
Define __init__ with self plus any parameters for initial data.
3
Assign instance attributes using self.attribute = value.
4
Add methods: functions with self that operate on the object's data.
5
Create instances by calling ClassName(args). Access attributes with dot notation.

Example 1: Student Class

class Student: def __init__(self, name, grade): self.name = name self.grade = grade def introduce(self): return f"Hi, I'm {self.name} in grade {self.grade}." # Create instances alice = Student("Alice", 10) bob = Student("Bob", 11) print(alice.introduce()) # Hi, I'm Alice in grade 10.

Example 2: BankAccount

class BankAccount: def __init__(self, owner, balance=0): self.owner = owner self.balance = balance def deposit(self, amount): self.balance += amount return self.balance def withdraw(self, amount): if amount > self.balance: return "Insufficient funds" self.balance -= amount return self.balance

Example 3: Tracking Instances

class Player: count = 0 # class attribute def __init__(self, username): self.username = username Player.count += 1 p1 = Player("Ace") p2 = Player("Nova") print(Player.count) # 2

Common Mistakes

✕ Forgetting self
Every method must have self as its first parameter. Without it, Python raises a TypeError when you call the method.
✕ Forgetting __init__
Without __init__, your instances start with no attributes. You can still add them later, but it defeats the purpose of the blueprint.
✕ Calling the class wrong
Writing Dog.bark() instead of my_dog.bark(). Methods operate on instances, not the class itself (unless using @classmethod).

Summary of Key Concepts

Concept Syntax Purpose
Define a class class Name: Create a new type
Initializer def __init__(self, ...): Set up instance data
Instance attribute self.attr = value Store unique data
Method def method(self): Define behavior
Create instance obj = Name(args) Build an object
Access attribute obj.attr Read/modify data

Question 1 of 5

Which keyword is used to define a new class in Python?

Question 2 of 5

What is the purpose of the __init__ method?

Question 3 of 5

What does my_car.make evaluate to?

Question 4 of 5

What is the value of Counter.total after all three instances are created?

Question 5 of 5

What happens when this code runs?