Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

Tutorial

Python Classes and Objects Explained: Blueprints, Instances, and Methods

Learn how Python classes act as blueprints, objects as real instances, and how methods and attributes bundle data with behavior. A practical guide with a real bank account example.

June 2026 · 7 min read · 1 views · 0 hearts

Everything in Python is an object. You’ve heard that before, but what does it actually mean when you’re writing real code? Understanding classes, objects, methods, and attributes is the key to moving beyond scripts that just run top to bottom. These aren’t fancy theory—they’re the building blocks of how Python models anything from a user account to a chess engine.

The Blueprint: What Is a Class?

Think of a class as a blueprint. It defines what something should be and what it should do, but it isn’t the thing itself. A blueprint for a house doesn’t keep you dry in the rain—you need the actual house. In Python, you define a class with the class keyword:

class Dog:
    pass

That’s a valid class. It does nothing, but it already lets you create objects. Classes give you structure; they let you group related data (attributes) and functions (methods) into a single concept.

The Real Thing: What Is an Object?

An object is an instance of a class—a concrete, living version of that blueprint. You create one by calling the class like a function:

my_dog = Dog()

my_dog is now a Dog object. It occupies memory. It can have its own data. You can create a hundred dogs, and each one is separate. The class defines the type; the object is the thing.

Attributes: The Data That Belongs to an Object

Attributes are variables attached to an object. They hold state. In real-world terms, your dog has a name, breed, age. In code, you attach attributes to an object with dot notation:

my_dog.name = "Rex"
my_dog.breed = "German Shepherd"
my_dog.age = 3

You can also set them inside the class using the special __init__ method, which runs automatically when you create an object:

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

rex = Dog("Rex", "German Shepherd", 3)

self is a reference to the object being created. It’s how the object talks to itself. When you say self.name = name, you’re saying “attach this name value to the object that’s being born right now.”

Attributes can be anything: integers, strings, lists, other objects. They’re not restricted.

Methods: Functions That Belong to an Object

A method is just a function that belongs to a class. It operates on the object’s data. You define it inside the class, and its first parameter is always self:

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

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

    def human_years(self):
        return self.age * 7

Now you can call methods on your object:

print(rex.bark())        # "Rex says Woof!"
print(rex.human_years()) # 21

Methods bundle behavior with data. Instead of having a function that takes a dog as an argument and prints something, the dog itself knows how to bark. This keeps your code organized and readable.

Putting It All Together: A Working Example

Let’s model a bank account. You want accounts to hold a balance, allow deposits and withdrawals, and show a history.

class BankAccount:
    def __init__(self, owner, initial_balance=0):
        self.owner = owner
        self.balance = initial_balance
        self.transactions = []

    def deposit(self, amount):
        self.balance += amount
        self.transactions.append(f"Deposit: +${amount}")
        return self.balance

    def withdraw(self, amount):
        if amount > self.balance:
            return "Insufficient funds"
        self.balance -= amount
        self.transactions.append(f"Withdrawal: -${amount}")
        return self.balance

    def statement(self):
        print(f"Account of {self.owner}")
        for tx in self.transactions:
            print(tx)
        print(f"Balance: ${self.balance}")

Create an account and use it:

alice_account = BankAccount("Alice", 1000)
alice_account.deposit(500)
alice_account.withdraw(200)
alice_account.statement()

Output:

Account of Alice
Deposit: +$500
Withdrawal: -$200
Balance: $1300

Notice how the object holds its own state (balance, transactions) and its own behavior (deposit, withdraw, statement). That’s the core idea of object-oriented programming in Python.

Common Gotchas to Keep in Mind

  • Don’t confuse class attributes and instance attributes. If you define an attribute directly in the class body (not in __init__), it’s shared across all objects. Instance attributes (set with self) are unique per object.
  • Methods always need self as the first parameter, even if they don’t use it. Forgetting it gives you a TypeError.
  • You can add attributes outside the class, but it’s messy. Stick to __init__ for consistency.

Why This Matters for Real Code

Once you grasp these four concepts, you can:

  • Build reusable components that bundle data and logic.
  • Model complex systems cleanly (users, products, games, sensors).
  • Work with Python libraries—everything in Python, from strings to modules to Pandas DataFrames, is an object.

Classes, objects, methods, and attributes aren’t Python’s whole story, but they’re the foundation. Get comfortable with how blueprints become real, living objects, and you’ll write code that’s not just correct but elegant.

Comments

Questions, corrections, and tips stay visible for everyone reading this page.

0 in thread

Join the discussion

Shown next to your comment.

Up to 4,000 characters

No comments yet

Be the first to leave a note — it helps the next reader.