🟡 Advanced Python · Lesson 21
Decorators in Python
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
| Point | Details |
|---|---|
| Course Area | Advanced Python Professional concepts used to make code reusable, clean and project-ready. |
| Main Use | adding logging or timing |
| Example File | decorators.py |
| Practice Focus | Run, 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.
| Term | Meaning |
|---|---|
| wrapper | Inner function used by a decorator to add extra behavior. |
| higher-order function | Function that accepts another function or returns a function. |
| @decorator | Syntax used to apply a decorator above a function. |
| logging | logging is an important term in this topic. |
| reuse | Using 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 wrappersends a result back from the function.@simple_loggerperforms 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
- Type the program in
decorators.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to adding logging or timing.
- 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.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.