---
title: "Quick Answer to Python Decorators"
description: "Python decorators are wrappers that add new capabilities to an original function without altering it."
url: https://srinimf.com/2023/02/11/how-to-answer-python-decorators-question-quickly/
date: 2023-02-11
modified: 2023-10-20
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/02/pexels-photo-1148450.jpeg?fit=1880%2C1253&ssl=1
categories: ["Python"]
tags: ["Python decorators"]
type: post
lang: en
---

# Quick Answer to Python Decorators

The decorator is a wrapper that adds extra features to the original function.

ON THIS PAGE

1. [Python decorators](https://srinimf.com/2023/02/11/how-to-answer-python-decorators-question-quickly/#python-decorators)
2. [Principle of decorator](https://srinimf.com/2023/02/11/how-to-answer-python-decorators-question-quickly/#principle-of-decorator)
3. [Decorator example](https://srinimf.com/2023/02/11/how-to-answer-python-decorators-question-quickly/#decorator-example)

## **Python decorators**

**Point:1**

A decorator is a wrapper around a function. It adds new abilities to the original function.

**Point:2**

Decorators accept the original function as an argument. This concept is also called **function pointers** in Python.

**Point:3**

Decorators do not change the original function. These wrap the existing ones.

## **Principle of decorator**

![Python Decorators](https://i0.wp.com/srinimf.com/wp-content/uploads/2023/02/a1.png?resize=411%2C271&ssl=1)

## **Decorator example**

A function **f** has been passed to the **hello** function. Inside of that added a **wrapper** function that adds additional functionality for the **f** function. Here **f** is the original function, and** hello** is the wrapper function.

The special syntax @hello, adds the wrapper’s functionality.

```
#Decorator
def hello(f):
    def wrapper(name):
        print("Hello ",end='' )
        f(name)
    return wrapper

@hello
def print_name(name):
    print(name)
 
print_name("XYZ")
```

Output

The output of the original function is XYZ. The decorator added the additional functionality of Hello to XYZ.

```
Hello XYZ

** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

Related

- [10 Python Interview Questions: SLK Software](https://srinimf.com/2024/05/11/10-python-interview-questions-slk-software/)DateMay 11, 2024
- [13 Essential Python Interview Questions](https://srinimf.com/2021/11/24/13-essential-python-interview-questions/)DateNovember 24, 2021
- [Python, PySpark, SQL Dump: All Interviews Questions](https://srinimf.com/2024/10/08/python-pyspark-sql-dump-all-interviews-questions/)DateOctober 8, 2024

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F02%2F11%2Fhow-to-answer-python-decorators-question-quickly%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F02%2Fpexels-photo-1148450.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=Quick%20Answer%20to%20Python%20Decorators)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/02/11/how-to-answer-python-decorators-question-quickly/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/02/11/how-to-answer-python-decorators-question-quickly/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/02/11/how-to-answer-python-decorators-question-quickly/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/02/11/how-to-answer-python-decorators-question-quickly/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Quick%20Answer%20to%20Python%20Decorators&body=https%3A%2F%2Fsrinimf.com%2F2023%2F02%2F11%2Fhow-to-answer-python-decorators-question-quickly%2F&share=email)
-

### Like this:

Like Loading…
