Skip to content

Function Decorator#

What Is a Decorated Function?#

The @ preceding a function is called a decorator. Such decoractor wraps a function and gives extra functionality without changing the original function.

The pattern is:

@decorator
def some_function():
    # Function code

Example#

Use the @benchmark_timer as function decorator to measure performance time:

Python
1
2
3
4
5
6
7
from timer import benchmark_timer

@benchmark_timer
def test_function():
    # Insert your code here

test_function()

Terminal output example:

Elapsed time: 12.34 seconds for thread TEST_FUNCTION