Chaining methods in Python#

What is method chaining?#

Method chaining is a programming style in which multiple method calls are made on the same object, in sequence. Each method call returns the object itself, so the next method can be called on it without having to assign the object to a variable in between.

This can make code more concise and readable, as it can be written as a single line of code instead of multiple lines. It can also make code more efficient, as the object does not have to be passed around between methods.

How to chain class methods in Python#

To chain class methods in Python, you simply need to call each method on the object, separated by a period. Let’s take a look at an example without method chaining:

Example counter.py without method chaining#
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

class Counter(object):
    def __init(self):
        self.count = 0

    def increment(self):
        self.count += 1

    def decrement(self):
        self.count -= 1

    def value(self):
        return self.count


def main():
    counter = Counter()
    counter.increment()
    counter.increment()
    counter.decrement()
    print(counter.value())


if __name__ == '__main__':
    main()

In this example, the increment() and decrement() methods are called separately, and the value() method is called after all method calls. This is not very efficient, as the object has to be passed around between all the different methods calls before value() is called.

When to use method chaining#

Method chaining can be used whenever you need to call multiple methods on the same object. It is particularly useful when the methods are related to each other, such as when you need to call a method to get some data and then call another method to do something with that data.

If we rewrite the previous example with method chaining by adding return self lines, it would look like this:

Example counter.py with method chaining#
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

class Counter(object):
    def __init(self):
        self.count = 0

    def increment(self):
        self.count += 1
        return self

    def decrement(self):
        self.count -= 1
        return self

    def value(self):
        return self.count


def main():
    counter = Counter()
    print(counter.increment().increment().decrement().value())


if __name__ == '__main__':
    main()

In the updated example, the increment() method is called first, followed by a second call of the increment() method, and then the decrement() method. After this the value() method is called to get the value of the counter without having to assign it to a variable.

Benefits and drawbacks of method chaining#

There are several benefits to using method chaining:

  • It can make code more concise and readable.

  • It can make code more efficient, as the object does not have to be passed around between methods.

  • It can make code more modular, as each method can be responsible for a single task.

There are a few drawbacks to using method chaining:

  • It can make code more difficult to debug, as it can be hard to track the order in which the methods are called.

  • It can make code more difficult to test, as each method call needs to be tested individually.

  • It can make code more difficult to understand for someone who is not familiar with the class.

Conclusion about method chaining#

Method chaining is a powerful programming technique that can be used to make code more concise, readable, and efficient. However, it is important to be aware of the potential drawbacks of method chaining before using it.