This tutorial provides an in-depth understanding of methods in Python. We’ll cover how to define methods, different types of methods, and when to use each type.

Beginner30 minutes

Step 1: Understanding Methods in Python

Methods in Python are essentially functions defined within a class. They are used to define the behaviors of an object.

Practice Exercise

Proceed to the next step

Show Solution

Step 2: Defining a Method in Python

A method is defined using the def keyword. It must be indented under the class definition.

class Vehicle:
    def start_engine(self):
        print('Engine started')

Practice Exercise

Define a 'stop_engine' method within the 'Vehicle' class.

Show Solution
class Vehicle:
    def stop_engine(self):
        print('Engine stopped')

Step 3: Understanding 'self' in Python Methods

'self' is a convention in Python that refers to the instance of the object itself. It allows us to access the object's attributes.

class Vehicle:
    def __init__(self, color):
        self.color = color
    def describe(self):
        print('The vehicle is '+self.color)

Practice Exercise

Create a method 'change_color' to change the color of the vehicle and print the new color.

Show Solution
class Vehicle:
    def change_color(self, new_color):
        self.color = new_color
        print('The vehicle color has been changed to '+self.color)

Step 4: Understanding Instance Methods

Instance methods are the most common type of methods in Python. They can modify the instance state, and can also access the class itself through the self.__class__ attribute.

class Vehicle:
    def __init__(self, color):
        self.color = color
    def get_color(self):
        return self.color

Practice Exercise

Create a method 'is_color_blue' that returns True if the color of the vehicle is blue, otherwise False.

Show Solution
class Vehicle:
    def is_color_blue(self):
        return self.color == 'blue'

Step 5: Understanding Class Methods

Class methods are bound to the class and not the instance of the object. They can't modify instance state but can modify class state. They are defined using the @classmethod decorator.

class Vehicle:
    count = 0
    def __init__(self):
        Vehicle.count += 1
    @classmethod
    def get_count(cls):
        return cls.count

Practice Exercise

Create a class method 'reset_count' that resets the vehicle count to zero.

Show Solution
class Vehicle:
    @classmethod
    def reset_count(cls):
        cls.count = 0

Step 6: Understanding Static Methods

Static methods can't modify the instance or the class state. They are bound to neither and work like regular functions but belong to the class's namespace. They are defined using the @staticmethod decorator.

class Vehicle:
    @staticmethod
    def make_noise():
        print('Vroom!')

Practice Exercise

Create a static method 'make_honk_sound' that prints 'Honk!'.

Show Solution
class Vehicle:
    @staticmethod
    def make_honk_sound():
        print('Honk!')

Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.

Sign in

Click to access the login or register cheese