Friday, 28 August 2026

๐Ÿš€ Day 100/150 – Decorator Example in Python


 

๐Ÿš€ Day 100/150 – Decorator Example in Python

A decorator is a special function in Python that allows you to add extra functionality to another function without modifying its original code. Decorators are commonly used for logging, authentication, timing functions, and more.

In this post, we'll explore four common examples of decorators in Python.


Method 1 – Basic Decorator

Create a simple decorator that prints a message before calling a function.

def decorator(func): def wrapper(): print("Before the function is called") func() return wrapper @decorator def greet(): print("Hello, World!") greet()









Output

Before the function is called 
Hello, World!

Explanation

  • decorator() accepts a function as an argument.

  • wrapper() adds extra functionality before calling the original function.

  • @decorator applies the decorator to greet().

  • Calling greet() actually executes wrapper().


Method 2 – Decorator with Function Arguments

Decorators can also work with functions that take parameters.

def decorator(func): def wrapper(name): print("Welcome!") func(name) return wrapper @decorator def greet(name): print("Hello,", name) greet("Alice")











Output
Welcome!
Hello, Alice

Explanation

  • wrapper(name) accepts the argument passed to greet().

  • It prints a welcome message before calling the original function.

  • The original function receives the same argument.


Method 3 – Decorator that Executes Code Before and After

A decorator can execute code both before and after the original function.

def decorator(func): def wrapper(): print("Starting...") func() print("Finished!") return wrapper @decorator def task(): print("Task is running") task()










Output

Starting... 
Task is running 
Finished!

Explanation

  • The decorator prints "Starting...".

  • It then calls the original function.

  • After the function finishes, it prints "Finished!".

  • This is useful for logging and monitoring function execution.


Method 4 – Taking User Input

Use a decorator with a function that accepts user input.

def decorator(func): def wrapper(name): print("Welcome to Python!") func(name) return wrapper @decorator def greet(name): print("Hello,", name) name = input("Enter your name: ") greet(name)












Sample Input

Sam

Output

Welcome to Python! 
Hello, Sam

Explanation

  • The user enters a name.

  • The decorator displays a welcome message.

  • The original function greets the user using the entered name.


Comparison of Methods

MethodBest For
Basic DecoratorUnderstanding how decorators work
Decorator with ArgumentsFunctions that accept parameters
Before and After ExecutionLogging and monitoring
User InputInteractive programs

๐Ÿ”ฅ Key Takeaways

  • A decorator adds extra functionality to a function without changing its original code.

  • Decorators are created using functions that return another function.

  • The @decorator syntax is used to apply a decorator.

  • Decorators can work with functions that have parameters.

  • They are commonly used for logging, authentication, timing, caching, and validation.

Stay tuned for Day 101 of the #150DaysOfPython series! ๐Ÿš€

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (342) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (351) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (90) Coursera (303) Cybersecurity (36) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (425) Data Strucures (18) Deep Learning (218) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (396) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1364) Python Coding Challenge (1229) Python Library (1) Python Mathematics (15) Python Mistakes (51) Python Quiz (615) Python Tips (106) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (20) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)