🟡 Advanced Python  ·  Lesson 21

Decorators in Python

Python Decorators

What are Decorators in Python?

Decorators in Python means a decorator adds extra behavior to a function without changing the original function code.

In real programs, this topic helps in adding logging or timing. Learn the idea first, then type the program yourself and compare the output.

💡 At a Glance
PointDetails
Course AreaAdvanced Python
Professional concepts used to make code reusable, clean and project-ready.
Main Useadding logging or timing
Example Filedecorators.py
Practice FocusRun, change values, and explain the output line by line.

Why should you learn this?

  • It is useful for adding logging or timing.
  • It connects with checking permissions.
  • It improves your ability to read, write and debug Python programs.

Important Terms

These terms are used directly in this lesson. Understand them before memorising the code.

TermMeaning
wrapperInner function used by a decorator to add extra behavior.
higher-order functionFunction that accepts another function or returns a function.
@decoratorSyntax used to apply a decorator above a function.
logginglogging is an important term in this topic.
reuseUsing a saved model again without retraining.

Syntax / Basic Pattern

The simple pattern is: prepare data, apply the concept, then show the result.

Basic Pattern
def simple_logger(func):
    def wrapper():
        print("Function started")
        func()
        print("Function ended")
    return wrapper
@simple_logger
def greet():

Complete Example Program

Python – decorators.py
def simple_logger(func):
    def wrapper():
        print("Function started")
        func()
        print("Function ended")
    return wrapper

@simple_logger
def greet():
    print("Hello Python")

greet()

Expected Output

Function started Hello Python Function ended

Program Explanation

  • def simple_logger(func): creates a reusable function.
  • def wrapper(): creates a reusable function.
  • print("Function started") displays information or calculated result on the screen.
  • func() performs the next step of the program logic.
  • print("Function ended") displays information or calculated result on the screen.
  • return wrapper sends a result back from the function.
  • @simple_logger performs the next step of the program logic.

Where will you use it?

  • Adding logging or timing.
  • Checking permissions.
  • Reusing extra behaviour.

Common Mistakes

  • Making code complex when a simple function or class is enough.
  • Not handling possible errors or edge cases.
  • Mixing project dependencies instead of using a virtual environment.

Practice Tasks

  1. Type the program in decorators.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to adding logging or timing.
  4. Write 5 lines explaining the logic in your own words.

Summary

Decorators in Python is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.

Python Decorators क्या है?

Python Decorators ka matlab hai: A decorator adds extra behavior to a function without changing the original function code. Simple words me, ye topic practical Python programs likhne me direct use hota hai.

Is topic ko sirf definition ke liye nahi, balki adding logging or timing jaise real examples ke liye practice karein.

यह क्यों सीखना जरूरी है?

  • Ye adding logging or timing me kaam aata hai.
  • Ye checking permissions se bhi connected hai.
  • Isse aap code ka output aur errors better samajh paate hain.

Important Terms

TermMeaning
wrapperInner function used by a decorator to add extra behavior.
higher-order functionFunction that accepts another function or returns a function.
@decoratorSyntax used to apply a decorator above a function.
logginglogging is an important term in this topic.
reuseUsing a saved model again without retraining.

Syntax / Basic Pattern

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

Basic Pattern
def simple_logger(func):
    def wrapper():
        print("Function started")
        func()
        print("Function ended")
    return wrapper
@simple_logger
def greet():

Complete Example Program

Python – decorators.py
def simple_logger(func):
    def wrapper():
        print("Function started")
        func()
        print("Function ended")
    return wrapper

@simple_logger
def greet():
    print("Hello Python")

greet()

Expected Output

Function started Hello Python Function ended

Program Explanation

  • def simple_logger(func): creates a reusable function.
  • def wrapper(): creates a reusable function.
  • print("Function started") displays information or calculated result on the screen.
  • func() performs the next step of the program logic.
  • print("Function ended") displays information or calculated result on the screen.
  • return wrapper sends a result back from the function.
  • @simple_logger performs the next step of the program logic.

Practical Uses

  • Adding logging or timing.
  • Checking permissions.
  • Reusing extra behaviour.

Common Mistakes

  • Making code complex when a simple function or class is enough.
  • Not handling possible errors or edge cases.
  • Mixing project dependencies instead of using a virtual environment.

Practice Tasks

  1. Program ko decorators.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. adding logging or timing par ek छोटा example banayen.
  4. Logic ko apne words me 5 lines me likhein.

सारांश

Decorators in Python ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.

← Back to Python Tutorial