Python Classes and Objects Explained

Classes and objects are a big part of Python, but the basic idea is simple.

A class is a blueprint. An object is one real thing made from that blueprint.

If that sounds abstract, do not worry. This page shows what classes and objects mean, how they work together, and how to use them in simple Python code.

Quick example

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

buddy = Dog("Buddy")
print(buddy.name)

Output:

Buddy

In this example:

  • Dog is the class
  • buddy is an object created from that class
  • name is data stored in the object

Use this as the basic mental model:

  • Class = blueprint
  • Object = one thing created from the blueprint

What this page covers

  • What a class means in simple terms
  • What an object means in simple terms
  • How classes and objects work together
  • The beginner basics you need before moving to more advanced OOP topics

What is a class?

A class is a way to group related data and behavior together.

You can think of a class as a template for making objects.

A class can contain:

  • Attributes: data stored in an object
  • Methods: functions that belong to the class

Here is a very small class:

class Dog:
    pass

This creates a class named Dog.

Right now, the class is empty. It does not store data or define behavior yet. But Python now knows that Dog is a class you can use to create objects.

If you want a shorter definition, see what is a class in Python.

What is an object?

An object is one value created from a class.

If Dog is the blueprint, then a real dog object is something made from it.

For example:

class Dog:
    pass

buddy = Dog()
max_dog = Dog()

print(type(buddy))
print(type(max_dog))

Output:

<class '__main__.Dog'>
<class '__main__.Dog'>

Here:

  • buddy is an object
  • max_dog is another object
  • both were created from the same class, Dog

Each object can store its own data. That is one reason classes are useful.

If you want a simple definition, see what is an object in Python.

Why classes are useful

Classes help when you want to keep related code together.

They are useful because:

  • They organize data and behavior in one place
  • They help model real things like dogs, cars, or students
  • They let you reuse the same structure many times
  • They make larger programs easier to understand

For very small scripts, you may not need classes. But as programs grow, classes often make code easier to manage.

If you want the bigger picture, see object-oriented programming in Python explained.

Basic class syntax

You define a class with the class keyword:

class ClassName:
    pass

A more useful class often includes methods:

class Dog:
    def bark(self):
        print("Woof!")

A few important points:

  • class Dog: starts the class definition
  • def bark(self): defines a method inside the class
  • self refers to the current object
  • Indentation matters inside the class

A method is just a function inside a class.

When you call a method on an object, Python automatically connects that object to self.

Creating objects from a class

You create an object by calling the class name like a function:

class Dog:
    pass

buddy = Dog()

Now buddy stores a new object.

You can then access attributes and methods with dot notation:

class Dog:
    def bark(self):
        print("Woof!")

buddy = Dog()
buddy.bark()

Output:

Woof!

This is the basic pattern:

  • define a class
  • create an object from it
  • use dot notation to work with it

For a step-by-step guide, see how to create an object in Python.

Attributes and methods

Attributes are the data stored in an object.

Methods are the actions an object can perform.

Here is a simple example that shows both:

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

    def bark(self):
        print(self.name + " says woof!")

buddy = Dog("Buddy")
print(buddy.name)
buddy.bark()

Output:

Buddy
Buddy says woof!

In this example:

  • self.name is an attribute
  • bark() is a method
  • buddy is the object using both

This is an important idea:

  • data belongs in attributes
  • behavior belongs in methods

The __init__ method

The __init__ method is a special method that runs when an object is created.

It is commonly used to give the object its starting values.

Example:

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

buddy = Dog("Buddy")
print(buddy.name)

When Dog("Buddy") runs:

  • Python creates a new object
  • __init__ runs automatically
  • "Buddy" gets assigned to self.name

You do not need __init__ in every class. Use it when you want to set up attributes when the object is made.

To learn this in more detail, see the __init__ method in Python explained.

Simple real example

Here is a short, beginner-friendly example using a Student class.

class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def introduce(self):
        print("Hi, my name is " + self.name)
        print("My grade is " + str(self.grade))

student1 = Student("Ava", 5)

print(student1.name)
print(student1.grade)
student1.introduce()

Output:

Ava
5
Hi, my name is Ava
My grade is 5

What this example shows:

  • Student is the class
  • student1 is an object
  • name and grade are attributes
  • introduce() is a method

This is the full beginner pattern in one example:

  1. Define a class
  2. Add __init__ to set starting values
  3. Create an object
  4. Access attributes with dot notation
  5. Call methods with dot notation

If you want to build one from scratch, see how to create a class in Python.

Common beginner mistakes

Here are some very common problems when learning classes and objects.

Forgetting self in a method

This causes errors because instance methods need self as the first parameter.

Wrong:

class Dog:
    def bark():
        print("Woof!")

Correct:

class Dog:
    def bark(self):
        print("Woof!")

Trying to use an attribute before setting it

If the attribute was never created, Python will raise an error.

Wrong:

class Dog:
    def bark(self):
        print(self.name)

If you create a Dog object and call bark() before setting self.name, the code will fail.

Correct:

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

    def bark(self):
        print(self.name)

Confusing the class with an object

A class is not the same thing as an object.

Wrong idea:

class Dog:
    pass

print(Dog.name)

Dog is the class itself. It does not automatically have object-specific data like name.

You usually need to create an object first:

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

buddy = Dog("Buddy")
print(buddy.name)

Indentation problems inside the class

Python uses indentation to show which code belongs inside the class and inside each method.

Wrong:

class Dog:
def bark(self):
    print("Woof!")

Correct:

class Dog:
    def bark(self):
        print("Woof!")

FAQ

What is the difference between a class and an object in Python?

A class is a blueprint. An object is one actual instance created from that blueprint.

Do I need classes as a beginner?

Not for every program, but classes become useful when you want to group data and behavior together.

What does self mean in a Python class?

self refers to the current object. It lets each object store and use its own data.

Is __init__ required in every class?

No. You only need it when you want to set up attributes when an object is created.

See also