Python Basic Class Example (OOP)

This page shows a simple Python class example for beginners.

You will learn how to:

  • Create a class
  • Make an object from that class
  • Store data in attributes
  • Add behavior with a method
  • Use __init__ to set starting values

If you are new to classes, this is a good first example because it uses a small real-world idea and keeps the code short.

Quick example #

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

    def bark(self):
        print(f"{self.name} says woof!")

my_dog = Dog("Max", 3)
print(my_dog.name)
print(my_dog.age)
my_dog.bark()

Output:

Max
3
Max says woof!

Use this as your first working class example. It shows:

  • A class
  • An __init__ method
  • Attributes
  • An object
  • A method call

What this example teaches #

This example helps you understand these basic OOP ideas:

  • A class is a template or blueprint
  • An object is one thing created from that class
  • Attributes store data for the object
  • Methods define what the object can do
  • __init__ sets the starting values when the object is created

If you want a fuller beginner explanation, see Python classes and objects explained.

The example class #

Here is the full class again before we break it down:

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

    def bark(self):
        print(f"{self.name} says woof!")

This class uses:

  • Dog as the class name
  • name and age as the two attributes
  • bark() as the one method

This is a good beginner example because it is small and easy to read.

How the __init__ method works #

The __init__ method runs when you create a new object.

In this example:

def __init__(self, name, age):
    self.name = name
    self.age = age

Here is what each part means:

  • __init__ is the special method Python uses to set up a new object
  • self refers to the current object
  • name and age are values passed in when the object is created
  • self.name = name stores the value in the object
  • self.age = age stores the age in the object

So if you create Dog("Max", 3), Python does this setup:

  • name becomes "Max"
  • age becomes 3
  • self.name is set to "Max"
  • self.age is set to 3

If self feels confusing, see the __init__ method in Python explained.

How to create an object #

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

my_dog = Dog("Max", 3)

This means:

  • Dog is the class
  • my_dog is the object
  • "Max" and 3 are passed into __init__

You can create more than one object from the same class:

dog1 = Dog("Max", 3)
dog2 = Dog("Bella", 5)

print(dog1.name)
print(dog2.name)

Output:

Max
Bella

Both objects use the same class, but each object can store different values.

class Dognameagebark()dog1 = Dog('Max', 3)dog2 = Dog('Bella', 5)
The Dog class is the blueprint; dog1 and dog2 are objects made from it.

You can learn more in how to create an object in Python.

How to access attributes and call methods #

Use dot notation to work with object data and behavior.

Read attributes #

my_dog = Dog("Max", 3)

print(my_dog.name)
print(my_dog.age)

Output:

Max
3

This reads stored data from the object.

Call a method #

my_dog = Dog("Max", 3)
my_dog.bark()

Output:

Max says woof!

This runs behavior defined in the class.

Data vs behavior #

  • my_dog.name reads data
  • my_dog.age reads data
  • my_dog.bark() runs a method

If you want more method examples, see basic methods in Python classes explained.

Step-by-step code breakdown #

Let’s go through the main lines one by one.

class Dog: #

class Dog:

This creates a new class named Dog.

Think of it as a blueprint for making dog objects.

def __init__(self, name, age): #

def __init__(self, name, age):

This defines the setup method for new objects.

When you create a Dog, Python runs this method automatically.

self.name = name #

self.name = name

This creates an attribute named name on the object.

The value passed in becomes part of that object.

def bark(self): #

def bark(self):
    print(f"{self.name} says woof!")

This defines a method named bark.

It uses self.name so the message matches the current object.

my_dog = Dog("Max", 3) #

my_dog = Dog("Max", 3)

This creates one Dog object and stores it in the variable my_dog.

At that moment:

  • self.name becomes "Max"
  • self.age becomes 3

Common beginner mistakes #

Here are some common problems beginners run into with classes.

Forgetting self in method definitions #

Wrong:

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

Right:

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

Inside a class, instance methods need self as the first parameter.

Forgetting parentheses when creating an object #

Wrong:

my_dog = Dog

Right:

my_dog = Dog("Max", 3)

Without parentheses, you are referring to the class itself, not creating an object.

Using a method name without () when trying to call it #

Wrong:

my_dog.bark

Right:

my_dog.bark()

Without (), Python gives you the method itself instead of running it.

Misspelling attribute names #

Wrong:

print(my_dog.nam)

Right:

print(my_dog.name)

A misspelled attribute name can cause an AttributeError: object has no attribute.

Bad indentation inside the class #

Wrong:

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

Right:

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

Python uses indentation to understand code blocks. Bad indentation can cause an IndentationError: expected an indented block.

Try it yourself #

Once the basic example works, try these small changes:

  • Change the object values
  • Create a second Dog object
  • Print both objects’ data
  • Add another method such as birthday()

Example:

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

    def bark(self):
        print(f"{self.name} says woof!")

    def birthday(self):
        self.age += 1
        print(f"{self.name} is now {self.age}")

dog1 = Dog("Max", 3)
dog2 = Dog("Bella", 5)

print(dog1.name, dog1.age)
print(dog2.name, dog2.age)

dog1.birthday()

Output:

Max 3
Bella 5
Max is now 4

This is a good next step after learning the basic example.

Common causes of confusion #

Beginners often get stuck on these points:

  • Confusing a class with an object
  • Not understanding what self refers to
  • Mixing up attributes and methods
  • Indentation mistakes inside the class body
  • Calling methods with missing required arguments

If that happens, test one small part at a time and print values often.

Useful debugging checks:

print(type(my_dog))
print(my_dog.name)
print(my_dog.age)
dir(my_dog)
help(Dog)

These can help you see:

  • What type my_dog is
  • Whether attributes exist
  • What names and methods are available on the object

FAQ #

What is the difference between a class and an object? #

A class is a blueprint. An object is one created item based on that blueprint.

Why do I need self in a class? #

self lets Python refer to the current object so each object can store its own data.

Is this page teaching all of OOP? #

No. This page focuses on one simple class example. Broader OOP concepts belong on separate learn pages.

Can a class have more than one method? #

Yes. A class can have many methods, but this example stays small to make the core idea clear.

See also #

After you understand this example, the next good step is to create your own class and then learn __init__ and methods in more detail.

Press Esc to close