---
title: "What are decorators in Python?"  
description: "What are decorators in Python?"  
author: "ICSM Computer"  
published: 2025-04-22  
updated: 2025-06-02  
canonical: https://www.mindstick.com/forum/161519/what-are-decorators-in-python  
category: "python"  
tags: ["python-3.4", "python"]  
reading_time: 2 minutes  

---

# What are decorators in Python?

What are [decorators](https://www.mindstick.com/interview/34380/explain-the-python-decorators) in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?

## Replies

### Reply by ICSM Computer

Decorators in Python are a powerful and elegant way to **modify or enhance the behavior of functions or methods** without changing their actual code.

### What is a decorator?

- A **decorator** is essentially a function that **takes another function as input** and **returns a new function** that adds some kind of functionality before or after the original function runs.
- You apply decorators using the `@decorator_name` syntax placed just above the function definition.

### Why use decorators?

- To add reusable functionality (e.g., logging, timing, access control) to many functions without repeating code.
- To keep your code clean and separate concerns.

### Basic example of a decorator

```python
def my_decorator(func):
    def wrapper():
        print("Before the function runs")
        func()
        print("After the function runs")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()
```

## Output:

```plaintext
Before the function runs
Hello!
After the function runs
```

### How it works:

`@my_decorator` is syntactic sugar for:

```python
say_hello = my_decorator(say_hello)
```

When you call `say_hello()`, it actually calls `wrapper()`, which runs extra code around the original function.

### Decorators with arguments

To decorate functions that take parameters, your wrapper function should accept `*args` and `**kwargs`:

```python
def decorator(func):
    def wrapper(*args, **kwargs):
        print("Before")
        result = func(*args, **kwargs)
        print("After")
        return result
    return wrapper

@decorator
def greet(name):
    print(f"Hello, {name}!")

greet("Alice")
```

### Summary

- Decorators wrap a function to extend its behavior.
- They’re commonly used for logging, memoization, access control, performance measurement, etc.
- Python includes built-in decorators like `@staticmethod`, `@classmethod`, and `@property`.


---

Original Source: https://www.mindstick.com/forum/161519/what-are-decorators-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
