## Introduction

What is decorator?

• A function that takes another function
• Extends the behavior of that function
• Without explicitly modifying the function

## Decorators

### Simple Decorators

Input:

Output:

Input:

Output:

Input:

Output:

Input:

• Time is 16:00 when I am running the following code.

Output:

### Returning Values From Decorated Functions

Return values from decorated functions don’t get returned by default unless the decorator allows it. In this lesson, you’ll see how to get return values out of decorated functions by making a small change to the decorator.

To make the function name more readable.

## Real-World Examples

### Timer example

What is the difference between !r and %r in Python?

In this lesson, you’ll see how to use decorators to measure the time a function takes to execute and print the duration to the console.

By using decorator @timer, we didn’t add or modify any code in function waste_some_time. And not only we get the returned value from waste_some_time, but also can we get the runtime for waste_some_time.

• Timer example is copied from Decorator Template

### Debugging Code With Decorators

In this lesson, you’ll see how to create a decorator for debugging code. You’ll see how the @debug decorator prints out the arguments a function is called with as well as its return value every time the function is called.

• Debug example is copied from Decorator Template

### Slowing Down Code With Decorators

Decorators can be used to slow code down. Slowing down code can be useful when working with web services or when you want to rate limit a function. In this lesson, you’ll see how to create a @slow_down decorator that sleeps one second before it calls the decorated function.

It’s useful when you are visiting resources such as API. with some limitations.

• Slowing Down example is copied from Decorator Template

### Registering Plugins With Decorators

The main benefit of registering Plugins with decorators is that you don’t have maintain a list of the functions you have created. You can easily know which plugins are exist.