Object-oriented programming (OOP) is one of the most popular paradigms. But some concepts involved in OOP are hard to grasp. There are three types of Python method, and their differences and purposes can often be confusing.

To write good Python OOP code, you should understand the difference between instance, static, and class methods.

The 3 Types of Methods in Python

There are three types of methods in Python: instance methods, static methods, and class methods.

You don't always have to consider these differences for every basic Python script you write. But when you're using OOP in Python to write more advance code, the differences can have big effects.

Understanding Decorator Patterns

Before looking at the differences, it's important to understand a design pattern known as the decorator pattern.

Decorators sound complex, but there's nothing to fear. Decorators are simply functions. You can write them yourself, or use those included in libraries, or the Python standard library.

Like any function, decorators perform a task. The difference here is that decorators apply logic or change the behavior of other functions. They are an excellent way to reuse code and can help to separate logic into individual concerns.

The decorator pattern is Python's preferred way of defining static or class methods. Here's what one looks like in Python:

        class DecoratorExample:
  """ Example Class """
  def __init__(self):
    """ Example Setup """
    print('Hello, World!')

@staticmethod
  def example_function():
    """ This method is decorated! """
    print('I\'m a decorated function!')

de = DecoratorExample()
de.example_function()

Decorators have to immediately precede a function or class declaration. They start with the @ sign, and unlike normal methods, you don't have to put parentheses on the end unless you are passing in arguments.

It's possible to combine decorators, write your own, and apply them to classes as well, but you won't need to do any of that for these examples.

Instance Methods in Python

Instance methods are the most common type of methods in Python classes. These are so-called because they can access unique data of their instance. If you have two objects each created from a car class, then they each may have different properties. They may have different colors, engine sizes, seats, and so on.

Instance methods must have self as a parameter, but you don't need to pass this in every time. Self is another Python special term. Inside any instance method, you can use self to access any data or methods that may reside in your class. You won't be able to access them without going through self. However, on top of self, you can include other parameters as well.

Finally, as instance methods are the most common, there's no decorator needed. When you create a Python class, its methods will be instance methods by default.

Here's an example:

        class DecoratorExample:
  """ Example Class """
  def __init__(self):
    """ Example Setup """
    print('Hello, World!')
    self.name = 'Decorator_Example'
 
  def example_function(self):
    """ This method is an instance method! """
    print('I\'m an instance method!')
    print('My name is ' + self.name)
 
de = DecoratorExample()
de.example_function()

The name variable is accessed through self. Notice that, when you call example_function, you don't have to pass self in—Python does this for you.

Static Methods in Python

Static methods are methods that are related to a class in some way, but don't need to access any class-specific data. You don't have to use self, and you don't even need to instantiate an instance, you can simply call your method:

        class DecoratorExample:
  """ Example Class """
  def __init__(self):
    """ Example Setup """
    print('Hello, World!')
 
@staticmethod
  def example_function():
    """ This method is a static method! """
    print('I\'m a static method!')
 
de = DecoratorExample.example_function()

The @staticmethod decorator was used to tell Python that this method is a static method.

Static methods are great for utility functions, which perform a task in isolation. They don't need to (and cannot) access class data. They should be completely self-contained, and only work with data passed in as arguments. You may use a static method to add two numbers together, or print a given string.

Class Methods in Python

Class methods are the third and final OOP method type to know. Class methods know about their class. They can't access specific instance data, but they can call other static methods.

Class methods don't need self as an argument, but they do need a parameter called cls. This stands for class, and like self, it gets automatically passed in by Python.

Class methods are created using the @classmethod decorator.

        class DecoratorExample:
  """ Example Class """
  def __init__(self):
    """ Example Setup """
    print('Hello, World!')
 
@classmethod
  def example_function(cls):
    """ This method is a class method! """
    print('I\'m a class method!')
    cls.some_other_function()
 
@staticmethod
  def some_other_function():
    print('Hello!')
  
de = DecoratorExample()
de.example_function()

Remember, instance methods can manipulate an object's state and have access to the class itself via self. Class methods, on the other hand, can't access the instance of a class but can access the class itself. This is the major difference between class and instance methods in Python.

Since class methods can manipulate the class itself, they are useful when you're working on larger, more complex projects.

Which Python Method Should You Use?

It may seem like a tough and daunting decision choosing between the types of methods in Python, but you'll soon get the hang of it with a bit of practice.

Even if you only write tiny scripts for fun, learning another OOP feature of Python is a great skill, and can help to make your code easier to troubleshoot, and easier to reuse in the future.