Classes -- Your First Blueprint

Learn how to define a class, the foundation of everything in OOP.

Introduction

In the previous lesson, you learned that OOP organizes code around objects. But before you can create objects, you need a class -- a blueprint or template that defines what those objects will look like and what they can do.

Think of it this way: you cannot build a house without a plan. The plan is the class. The actual house you build from it is the object.

What is a Class?

A class is a template that defines the structure and behavior that its objects will have. It specifies what data (attributes) and what actions (methods) belong to that type of thing.

Analogy: A cookie cutter is a class. It defines the shape. Every cookie you press out of it is an object. The cookies all have the same shape (structure), but each one might have different toppings (data).

Creating Your First Class

In Python, you create a class using the class keyword. Here is the simplest possible class:

class Dog:
    pass

That is it! The pass keyword is a placeholder that means "nothing here yet." You now have a class called Dog, even though it does not do anything yet.

Adding Attributes

Let us give our Dog class some data. These are called class attributes -- variables defined directly inside the class:

class Dog:
    name = "Buddy"
    breed = "Labrador"

Now every Dog created from this class will start with the name "Buddy" and the breed "Labrador." We will learn better ways to set individual attributes in Week 3 when we cover constructors.

Class Naming Convention

In Python, class names follow PascalCase -- each word starts with a capital letter, with no underscores:

# Good - PascalCase
class Dog:
class BankAccount:
class StudentRecord:

# Bad - not PascalCase
class dog:           # lowercase
class bank_account:  # snake_case
class studentrecord: # no capitals

A More Complete Class

Here is a class with multiple attributes:

class Student:
    name = "Alice"
    age = 20
    grade = "A"

# Let's verify it works
print(Student.name)   # Alice
print(Student.age)    # 20
print(Student.grade)  # A

You can access class attributes using the class name followed by a dot and the attribute name. In the next lesson, you will learn to create individual objects from this class.

OOP Terms

Class: A blueprint that defines the structure (attributes) and behavior (methods) for a type of object. It is a template, not an actual thing -- like a recipe is not the actual cake.

Real-World Connections

Classes model real things. Here are some examples of what classes you might create:

  • Car class: Attributes like brand, color, year, speed.
  • Product class: Attributes like name, price, category, stock.
  • Employee class: Attributes like name, department, salary, position.

Best Practices

  • Always use PascalCase for class names.
  • Give your classes clear, descriptive names that reflect what they represent.
  • Keep classes focused -- a Car class should not contain student data.

Common Mistakes

  • Forgetting the colon : at the end of the class definition line.
  • Using lowercase or snake_case for class names instead of PascalCase.
  • Leaving a class completely empty without pass -- Python will throw a syntax error.

Practice Exercises

  1. Create a class called Book with attributes title, author, and pages.
  2. Create a class called Movie with attributes for title, director, and year.
  3. Create a class called PhoneContact with attributes for name, phone number, and email. Print each attribute.

Summary

  • A class is a blueprint or template for creating objects.
  • Use the class keyword to define a class, followed by a colon.
  • Class names should use PascalCase.
  • Attributes are variables defined inside a class that hold data.
  • Use pass as a placeholder for empty classes.