Method (Python)

A method in Python is a function defined inside a class using the def keyword.

This page covers Method in Python. For a language-agnostic introduction, see Method.

Defining a Method#

Every instance method in Python takes self as its first parameter. self is a reference to the object the method is being called on.

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

By convention, method names use snake_case: greet, play, get_score.

Return Values#

Use return to send a value back from a method. A method without a return statement returns None.

class Player:
    def add(self, a, b):
        return a + b

Parameters#

Parameters come after self, separated by commas.

class Player:
    def greet(self, name):
        print(f"Hello, {name}!")

Calling a Method#

Call a method on an object using dot notation. Python passes self automatically — you don’t include it in the call.

player = Player()
player.greet("Alice")

Common Mistakes#

Forgetting self as the first parameter Every instance method must have self as its first parameter. Omitting it causes a TypeError when the method is called, because Python passes the object as the first argument automatically: greet() takes 0 positional arguments but 1 was given.

Passing self explicitly when calling the method When calling player.greet("Alice"), don’t write player.greet(player, "Alice"). Python handles self automatically — passing it manually causes a TypeError.

Using PascalCase for method names Python convention is snake_case for methods. Writing def Greet(self) works but goes against the style the Python community expects.

Forgetting the colon after def def greet(self) must end with a colon. Missing it is a SyntaxError.

Resources#