How to Add Methods to a Class in Python

A method is a function defined inside a class. In Python, you add methods to describe what an object can do.

On this page, you will learn how to:

  • Define a method inside a class
  • Call that method on an object
  • Pass extra data into a method
  • Return a value from a method
  • Understand why the first parameter is usually self

Quick answer

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

my_dog = Dog()
my_dog.bark()

Output:

Woof!

Define the method inside the class, include self as the first parameter, then call it on an object.

What this page helps you do

By the end, you will be able to:

  • Add a method to a class
  • Call the method from an object
  • Pass data into a method with parameters
  • Return a value from a method

If you are new to classes, it may help to read Python classes and objects explained first.

Basic method syntax

A method is a function inside a class.

Here is the basic pattern:

class ClassName:
    def method_name(self):
        print("Hello")

Important points:

  • The method must be indented inside the class
  • The first parameter is usually self
  • self refers to the current object
  • You call the method with dot notation, like object.method_name()

Simple example: add one method

Let’s create a class with one method and call it.

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

my_dog = Dog()
my_dog.bark()

Output:

Woof!

What this code does:

  • class Dog: creates a class
  • def bark(self): adds a method to that class
  • my_dog = Dog() creates an object
  • my_dog.bark() calls the method on that object

If you need help with object creation, see how to create an object in Python.

How self works

self is how a method refers to the current object.

When you write this:

my_dog.bark()

Python automatically passes my_dog into the method as the first argument.

So this method:

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

receives the object in self.

Why this matters:

  • self lets the method read data from the object
  • self lets the method change data in the object
  • Without self, instance data cannot be accessed correctly

You do not normally pass self yourself. Python does that when you call the method on an object.

Add a method that uses instance variables

Methods often work with data stored in the object.

A common way to store that data is with __init__. If that is new to you, see the __init__ method in Python explained.

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

    def describe(self):
        print(f"{self.name} is {self.age} years old.")

my_dog = Dog("Buddy", 3)
my_dog.describe()

Output:

Buddy is 3 years old.

Key lines:

  • self.name = name stores the name in the object
  • self.age = age stores the age in the object
  • self.name and self.age are used inside describe()

Without self, Python would treat name and age as regular local variables, not object data.

Add a method with extra parameters

A method can take more arguments after self.

Example:

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

    def greet(self, other_name):
        print(f"Hello {other_name}, I am {self.name}.")

person1 = Person("Maya")
person1.greet("Leo")

Output:

Hello Leo, I am Maya.

Here:

  • self refers to the current object
  • other_name is extra input passed into the method

Use this pattern when the method needs outside data to do its job.

Add a method that returns a value

A method can either print something or return something.

Example that prints

class Calculator:
    def double(self, number):
        print(number * 2)

calc = Calculator()
calc.double(5)

Output:

10

This shows the result, but you cannot reuse it later.

Example that returns

class Calculator:
    def double(self, number):
        return number * 2

calc = Calculator()
result = calc.double(5)
print(result)

Output:

10

Use return when you want to save the result, reuse it, or combine it with other code.

If you want more practice with functions first, see how to create a simple function in Python.

Common beginner mistakes

Here are some common problems when adding methods to a class.

Forgetting self

This code is wrong:

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

If you call it like this:

my_dog = Dog()
my_dog.bark()

You will get a TypeError because Python passes the object automatically, but the method does not accept it.

Fix it by adding self:

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

Related help: TypeError: missing required positional argument

Wrong indentation

Methods must be indented inside the class.

Wrong:

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

Correct:

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

Related help: IndentationError: expected an indented block

Using instance variables without self

Wrong:

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

    def speak(self):
        print(name)

This fails because name is not defined inside speak().

Correct:

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

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

Calling the method incorrectly

Usually, you should call an instance method on an object:

my_dog.bark()

Not on the class itself unless you know exactly what you are doing.

When to add a method

Add a method when an action belongs to the object.

For example, a bank account has data like a balance, and it can do actions like deposit money.

class BankAccount:
    def __init__(self, balance):
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def show_balance(self):
        print(self.balance)

account = BankAccount(100)
account.deposit(50)
account.show_balance()

Output:

150

This is useful because:

  • The data and action stay together
  • The class is easier to understand
  • The object manages its own state

If you have not created a class before, see how to create a class in Python.

Debugging tips

If your method is not working, these checks can help:

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

What they do:

  • print(type(my_object)) shows the object's type
  • print(dir(my_object)) shows available attributes and methods
  • help(MyClass) shows information about the class
  • print(my_object.__dict__) shows the object's stored instance variables

These are useful when:

  • You are not sure whether the object was created correctly
  • You want to check whether the method exists
  • You want to inspect stored values like name or age

FAQ

Do all class methods need self?

Instance methods do. The self parameter gives access to the current object. Static methods and class methods work differently.

Can I add more than one method to a class?

Yes. A class can have many methods, each handling a different action.

What is the difference between a function and a method?

A method is a function defined inside a class and usually works with objects created from that class.

Can a method return a value?

Yes. Use return when you want the method to give data back.

See also

Once you can add one method, the next step is building a small class with attributes, an __init__ method, and multiple methods that work together.