The DRY Principle with Decorators

Learn how the Don't Repeat Yourself (DRY) principle works with decorators.

We have seen how decorators allow us to abstract away certain logic into a separate component. The main advantage of this is that we can then apply the decorator multiple times to different objects in order to reuse code. This follows the DRY principle since we define certain knowledge once and only once.

The retry mechanism

The retry mechanism is a good example of a decorator that can be applied multiple times to reuse code. Instead of making each particular function include its own retry logic, we create a decorator and apply it several times. This makes sense once we have made sure that the decorator can work with methods and functions equally.

Get hands-on with 1200+ tech skills courses.