How to Create a Class in Python

If you want to group related data and actions together, a class is a good tool to use.

This page shows you how to create your first Python class step by step. You will learn the basic class structure, how to add attributes, how to add methods, and how to create objects from the class.

Quick answer

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

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

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

Output:

Max
Max says woof

Use class to define the blueprint, __init__ to set starting values, and ClassName(...) to create an object.

What this page helps you do

  • Create a simple class from scratch
  • Understand the basic class syntax
  • Add data to a class with attributes
  • Add behavior to a class with methods
  • Create objects from the class

When to use a class

Use a class when:

  • You want to group related data and actions
  • Many objects should share the same structure
  • You are modeling real things like Dog, Car, Student, or BankAccount

Do not use a class for very small one-off values. For example, if you only need a single number or string, a normal variable is enough.

If you are new to this topic, see Python classes and objects explained for a beginner-friendly overview.

Basic syntax for creating a class

To create a class:

  • Start with the class keyword
  • Write the class name in PascalCase, such as Person or Car
  • Add a colon : at the end
  • Indent everything inside the class

Here is the smallest possible class:

class Person:
    pass

pass means “do nothing for now.” It lets Python accept an empty class.

Add an __init__ method

The __init__ method runs when you create a new object.

You usually use it to set starting values for that object.

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

Important parts:

  • __init__ is a special method
  • self refers to the current object
  • name and age are values passed in when the object is created
  • self.name and self.age store those values in the object

If you want a deeper explanation, read the __init__ method in Python explained.

Create instance attributes

Instance attributes store data for each object.

You create them with self.attribute_name.

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

In this example:

  • self.name = name stores the person's name
  • self.age = age stores the person's age

Each object can have different values:

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

person1 = Person("Ana", 25)
person2 = Person("Ben", 31)

print(person1.name)
print(person2.name)

Output:

Ana
Ben

Add methods to the class

Methods are functions inside a class.

Use methods to define what an object can do.

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

    def introduce(self):
        print("Hi, my name is", self.name, "and I am", self.age, "years old.")

Notice that the method also uses self as the first parameter.

That gives the method access to the object's attributes.

If you want more practice with this part, see how to add methods to a class in Python.

Create an object from the class

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

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

person1 = Person("Liam", 20)

What happens here:

  • Person is the class
  • person1 is an object made from that class
  • "Liam" goes into name
  • 20 goes into age

Python passes the new object to self automatically.

For more on this step, read how to create an object in Python.

Access attributes and call methods

Use dot notation to work with objects.

Access an attribute

print(person1.name)
print(person1.age)

Call a method

person1.introduce()

Dot notation means:

  • person1.name gets data from the object
  • person1.introduce() runs a method on the object

Simple beginner example to include

Here is a complete beginner example using a Person class.

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

    def introduce(self):
        print("Hi, my name is", self.name, "and I am", self.age, "years old.")

person1 = Person("Sara", 22)

print(person1.name)
print(person1.age)
person1.introduce()

Output:

Sara
22
Hi, my name is Sara and I am 22 years old.

What this code does:

  • Defines a class named Person
  • Adds two attributes: name and age
  • Adds one method: introduce()
  • Creates one object named person1
  • Prints the attribute values
  • Calls the method

Explain class names and style

A few naming rules will make your code easier to read.

  • Class names usually use PascalCase
    • Example: Person, BankAccount, StudentRecord
  • Method names usually use snake_case
    • Example: introduce, get_balance, study_more
  • Attribute names usually use snake_case
    • Example: first_name, account_number

Try to choose names that clearly match the real thing you are modeling.

Good names:

  • Dog
  • Car
  • Student

Less helpful names:

  • A
  • X
  • Thing

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

Common mistakes

These are some common beginner mistakes when creating a class.

Forgetting the colon

This causes a syntax error:

class Person
    pass

Correct version:

class Person:
    pass

Not indenting code inside the class

Python requires indentation after the class line.

Wrong:

class Person:
print("hello")

Correct:

class Person:
    print("hello")

If you get this wrong, you may see IndentationError: expected an indented block.

Leaving out self in methods

Wrong:

class Person:
    def introduce():
        print("Hello")

Correct:

class Person:
    def introduce(self):
        print("Hello")

Using name instead of self.name

Wrong:

class Person:
    def __init__(self, name):
        name = name

This does not store the value in the object.

Correct:

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

Trying to access attributes before defining them

Wrong:

class Person:
    def show_name(self):
        print(self.name)

If self.name was never created, Python will fail.

Correct:

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

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

If this happens, you may see AttributeError: object has no attribute.

Misspelling __init__

These are wrong:

  • __int__
  • init
  • _init_

Correct spelling:

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

Debugging tips

If your class is not working, these commands can help you inspect the object.

print(type(my_object))
print(my_object.__dict__)
print(dir(my_object))
help(MyClass)

What they do:

  • type(my_object) shows the object's class
  • my_object.__dict__ shows its attributes
  • dir(my_object) lists available attributes and methods
  • help(MyClass) shows basic information about the class

FAQ

What is the difference between a class and an object?

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

You can also read what is a class in Python and what is an object in Python.

Do I always need __init__ in a class?

No. But it is the usual way to set starting attribute values.

Why do methods use self?

self gives the method access to the current object's attributes and other methods.

Can I create a class with no attributes?

Yes. A class can contain only methods or even be empty at first.

Should beginners learn classes early?

Yes, after functions and basic data types. Start with small real-world examples.

See also

After you can define a simple class, the next good step is to practice creating objects and adding your own methods with confidence.