Basic Methods in Python Classes Explained

Methods are one of the most important parts of Python classes.

A method is a function written inside a class. You use methods to describe actions that an object can perform. This page explains what methods are, why self is used, and how to create and call simple instance methods.

Quick example

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

my_dog = Dog()
my_dog.bark()

Output:

Woof!

Use a method when you want an object to perform an action. The self parameter refers to the current object.

What this page covers

  • Define a method in simple words
  • Show how methods belong to a class
  • Explain why methods use self
  • Show how to call a method on an object

What is a method in Python?

A method is a function inside a class.

If you are still getting used to classes, see Python classes and objects explained.

Methods are used to describe behavior. For example:

  • A Dog object might have a bark() method
  • A Person object might have a greet() method
  • A Car object might have a start() method

You call a method with dot notation:

object_name.method_name()

Here is a simple example:

class Lamp:
    def turn_on(self):
        print("The lamp is now on.")

lamp = Lamp()
lamp.turn_on()

Output:

The lamp is now on.

Methods help keep data and behavior together. That is one of the main ideas behind classes.

Why self is used

In an instance method, self refers to the current object.

That means:

  • self lets the method work with that object
  • self can read the object's attributes
  • self can change the object's attributes

A basic instance method looks like this:

def method_name(self):
    ...

Python automatically passes the object as the first argument when you call the method on an object.

Example:

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

my_dog = Dog()
my_dog.bark()

When Python runs my_dog.bark(), it automatically uses my_dog as self.

You do not write this:

my_dog.bark(my_dog)

Python handles that for you.

How to write a basic method

To create a basic method:

  1. Create a class with class ClassName:
  2. Define a method with def method_name(self):
  3. Indent the method inside the class
  4. Create an object
  5. Call the method with object.method_name()

Example:

class Person:
    def greet(self):
        print("Hello!")

person = Person()
person.greet()

Output:

Hello!

Key parts of this example

  • class Person: creates the class
  • def greet(self): creates a method
  • person = Person() creates an object
  • person.greet() calls the method

If you want more practice with creating behaviors in classes, see how to add methods to a class in Python.

Methods can use object data

Methods often work with attributes such as self.name or self.age.

This is what makes objects useful. Each object can store its own data, and methods can use that data.

Example:

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

    def greet(self):
        print("Hello, my name is", self.name)

person1 = Person("Ava")
person2 = Person("Liam")

person1.greet()
person2.greet()

Output:

Hello, my name is Ava
Hello, my name is Liam

The greet() method works differently for each object because each object has its own name.

If __init__ is new to you, read the __init__ method in Python explained.

Example: changing object data

A method can also update an attribute:

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

    def update_age(self, new_age):
        self.age = new_age

person = Person(20)
print(person.age)

person.update_age(21)
print(person.age)

Output:

20
21

Here, update_age() changes the data stored in the object.

Methods can return values

A method does not have to only print something.

Sometimes it is better to use return so the method gives a value back. This is more flexible because you can store the result in a variable or use it in another expression.

Example:

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

shape = Rectangle(4, 3)
result = shape.area()

print(result)

Output:

12

This method returns the area instead of printing it directly.

That makes it easier to reuse:

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

shape = Rectangle(5, 2)
print(shape.area() + 10)

Output:

20

Common beginner mistakes

Here are some common problems beginners run into with methods.

Forgetting self in the method definition

Wrong:

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

Correct:

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

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

Calling a method on the class instead of an object

Wrong:

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

Person.greet()

This can cause an error because no object is being passed as self.

Correct:

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

person = Person()
person.greet()

If you see an error like this, read how to fix TypeError: missing required positional argument.

Writing person.greet instead of person.greet()

This:

person.greet

does not call the method. It only refers to the method.

To run it, use parentheses:

person.greet()

Trying to use name instead of self.name

Wrong:

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

    def greet(self):
        print(name)

Correct:

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

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

Inside a method, object attributes should usually be accessed through self.

Using the wrong indentation

Python uses indentation to define blocks.

Wrong indentation inside a class can cause errors or make your method part of the wrong block.

Correct structure:

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

When to learn next

After basic methods, these are good next topics:

Debugging tips

If your method is not working, these can help:

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

What these do

  • print(type(my_object)) shows what kind of object you created
  • print(dir(my_object)) shows available attributes and methods
  • help(MyClass) shows information about the class
  • print(my_object.__dict__) shows the object's current attributes

If Python says an object does not have a method or attribute you expected, see how to fix AttributeError: object has no attribute.

FAQ

What is the difference between a function and a method in Python?

A function is standalone. A method is a function defined inside a class and usually works with an object.

Do all methods need self?

Instance methods do. Python uses self to refer to the current object.

Can a method return a value?

Yes. A method can return values just like a normal function.

Why do I get an error about a missing positional argument self?

This usually happens when the method is called incorrectly on the class instead of on an object, or when self was left out of the definition.

See also