Skip to content

method

In Python, a method is a function that is associated with a particular object or class. Methods are typically defined within a class and can be called on objects of that class.

Methods define the behaviors and actions that an object of a class can perform. Methods can take arguments. When you call a method on an instance, Python automatically passes that instance as the method’s first argument, which is conventionally named self. That reference is what allows the method to access and modify the object’s attributes.

Methods are a fundamental part of object-oriented programming in Python. They enable encapsulation and the manipulation of an object’s state.

Example

Here’s an example of a method within a class:

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

    def speak(self):
        return f"{self.name} says Woof!"

# Usage
dog = Dog("Buddy")
print(dog.speak())  # Output: Buddy says Woof!

In this example, the .speak() method is defined within the Dog class and can be called on any instance of Dog. When dog.speak() is called, it returns the string "Buddy says Woof!", demonstrating how methods can interact with object attributes.

Step through the call below to watch Python turn dog.speak() into Dog.speak(dog) and bind self to the instance that made the call:

Interactive diagram — enable JavaScript to view.

The widget opens on step 5 of 5. Click Replay, then Next, to rebuild the trace one row at a time: You call, Python runs, self is, self.name, and Returns. The highlights in the code and on the instance card track each step.

Then switch Call .speak() on from dog to pup. The method body doesn’t change, but the self badge moves to the pup card, self.name reads "Daisy", and the call returns "Daisy says Woof!". Whichever instance makes the call becomes self.

Python Classes: The Power of Object-Oriented Programming

Tutorial

Python Classes: The Power of Object-Oriented Programming

Learn how to define and use Python classes to implement object-oriented programming. Dive into attributes, methods, inheritance, and more.

intermediate best-practices python

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Oct. 21, 2026 • Reviewed by Dan Bader