Python function decorators are a powerful tool to enhance your code's readability and maintainability. They allow you to wrap another function in order to extend its behavior, without permanently modifying it. In this tutorial, we will explore the concept of Python function decorators, their syntax, and how to create and use them.

Before diving into decorators, let's ensure you have a basic understanding of higher-order functions and closures. Python allows functions to accept other functions as arguments and even return functions as outputs. These are known as higher-order functions, and they form the foundation of decorators.

What are Function Decorators?
In simplest terms, a function decorator is a function that takes in a function and returns another function. The returned function is said to be decorated. Decorators provide a way to modify or enhance the behavior of other functions or methods in Python.

Decorators follow the syntax: @
Simple Decorator Example

A simple decorator might change the behavior of a function based on some condition. Here's an example that adds a timestamp to each function call:
def timestamp_decorator(func):
def wrapper():
import datetime
print(f"Timestamp: {datetime.datetime.now()}")
func()
return wrapper
@timestamp_decorator
def say_hi():
print("Hi there!")
say_hi()This will output the timestamp followed by "Hi there!".

Decorating Functions with Arguments
Decorators can also handle functions with arguments. Let's modify our timestamp decorator to include the function name:
def timestamp_decorator(func):
def wrapper(*args, **kwargs):
import datetime
print(f"{func.__name__} was called at {datetime.datetime.now()}")
return func(*args, **kwargs)
return wrapper
@timestamp_decorator
def add_numbers(a, b):
return a + b
Now, our decorator works with functions having arguments.
Decorators with Arguments



















Sometimes, you might want to pass arguments to your decorators. You can do this by defining your decorator to accept an additional argument:
def repeat(n):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(n):
func(*args, **kwargs)
return wrapper
return decorator
@repeat(3)
def say_hi():
print("Hi there!")
This will print "Hi there!" three times.
Built-in Function Decorators
Python has several built-in decorators, such as @property, @staticmethod, and @classmethod. We won't go into detail about them here, but understanding their use can greatly enhance your coding skills.
property: Allows you to define getter, setter, and deleter methods for an instance variable. @property is used to define a getter method.
staticmethod: Indicates that the method does not change the state of the object (instance) it is called on. @staticmethod is used to define a method that belongs to the class rather than an instance.
classmethod: Similar to staticmethod, but it can access the class itself (via cls). @classmethod is used to define a method that belongs to the class rather than an instance.
Now that you understand the basics of Python function decorators, you can use them to create cleaner, more readable, and more maintainable code. Keep practicing and exploring more advanced topics like decorator composition and "decorators on top of decorators". Happy coding!