Seedha content par jao
🤖 Python Hindi Bot
Namaste! Main Python Hindi Bot hoon 🐍
Python ya AI se related kuch bhi type karke pucho, ya neeche se quick sawaal chuno.
WhatsApp

Stay Connected with PythonHindii

Join our WhatsApp Community to get the latest Python tutorials, coding tips, interview questions, project updates, and learning resources directly on your phone.

Join WhatsApp Community
⇧
Python Hindi Logo
Python in Hindi | Programming Education
Python programming tutorial in Hindi banner Python code learning illustration Hindi Python student coding illustration Python data code pattern background

Python Tutorial in HindiiLearn Python Programming for Free

Learn Python from Beginner to Advanced in simple Hindi.

print("Namaste Python!")
# Variable banana
naam = "Ankit"
umar = 25
 
for i in range(3):
  print("Sikho!")
 
# Output:
Sikho! Sikho! Sikho!

Why Students Learn Here

📚

Structured Tutorials

Beginner se Advanced tak step-by-step organized lessons.

💻

Practical Code

Real examples aur hands-on code jo actually samajh aaye.

🧠

Python + AI Path

Ek clear roadmap jo Python se AI tak le jaata hai.

🚀

Projects & Career

Portfolio-ready projects aur interview preparation.

Ye Kaise Kaam Karta Hai

Hindi mein Python, AI aur Data Skills seekhne ka sabse simple tareeka.

📘

Seekhein

Beginner se Advanced tak, best curated tutorials aur guides Hindi mein.

💻

Practice Karein

Real code examples aur mini-projects ke through hands-on practice karein.

💬

Judein

WhatsApp community se judkar apne doubts clear karein, akele nahi seekhenge.

Python to AI Learning Roadmap

Step-by-step learning path from Python basics to AI, Data Science and career-ready projects.

1
🐍

Python Basics

Introduction, Variables, Data Types, Operators, Input & Output, Conditions, Loops, Functions

Start Python →
2
💻

Python Intermediate

Lists, Tuples, Sets, Dictionaries, Strings, Functions, Modules, File Handling, Exception Handling

Continue Python →
3
⚙️

Advanced Python

OOP, Iterators, Generators, Decorators, Regular Expressions, APIs, Automation, Virtual Environments

Learn Advanced Python →
4
🗄️

SQL & Databases

SQL Basics, MySQL, SELECT, INSERT, UPDATE, DELETE, JOIN, GROUP BY, Database Projects

Learn SQL →
5
📊

Data Analytics

NumPy, Pandas, Data Cleaning, Data Visualization, Matplotlib, Exploratory Data Analysis

Learn Data Analytics →
6
🤖

Machine Learning

ML Basics, Supervised & Unsupervised Learning, Regression, Classification, Model Evaluation, Scikit-learn

Learn Machine Learning →
7
🧠

Artificial Intelligence

AI Fundamentals, Generative AI, AI Tools, Prompt Engineering, LLM Concepts, AI Automation

Explore AI →
8
🚀

Projects & Career

Python Projects, AI Projects, Data Projects, Portfolio, GitHub, Interview Preparation, Career Roadmap

Build Your Career →

Python Decorators Complete Guide | Decorator Syntax in Hindi | Python Hindi

Python Decorators Complete Guide in Hindi

Python Decorators

Python में कई बार हमें किसी existing function का code बदले बिना उसमें extra behavior जोड़ना होता है — जैसे function चलने से पहले या बाद में कुछ extra काम करवाना। इसके लिए Python का Decorator concept इस्तेमाल होता है।

Decorator को समझने के लिए Functions को value की तरह treat करना, Function को argument की तरह pass करना, और Inner Functions जैसे concepts पहले से समझ लेना बहुत मदद करता है। अगर आपने अभी First Class Function in Python नहीं पढ़ा है, तो पहले वो article पढ़ना बेहतर रहेगा। Examples खुद run करने के लिए हमारा Online Python Editor & Compiler use कर सकते हैं।

🎥 Video में पूरा Topic समझें

Decorator क्या है?

Decorator Python का एक ऐसा feature है जिससे किसी existing function के code को बदले बिना, उसमें extra functionality जोड़ी जा सकती है।

सरल भाषा में — Decorator किसी function को "wrap" करके उसे नई क्षमता दे देता है, बिना उसके अंदर का code छेड़े।
Example
def my_decorator(func): def wrapper(): print("Function चलने से पहले") func() print("Function चलने के बाद") return wrapper @my_decorator def greet(): print("Hello Python!") greet()

Output:

Function चलने से पहले Hello Python! Function चलने के बाद
Try it Yourself »

Python Decorator की जरूरत क्यों पड़ती है?

मान लीजिए आपके पास कई functions हैं और आप चाहते हैं कि हर function के चलने का time log हो, या हर function call से पहले कोई check हो जाए। बिना decorator के आपको हर function के अंदर वही code बार-बार लिखना पड़ेगा।

  • Code Repeat करने से बचाना — एक जैसी extra functionality कई functions में बार-बार नहीं लिखनी पड़ती
  • Original Function को साफ रखना — main logic और extra behavior अलग-अलग रहते हैं
  • Reusability — एक decorator को किसी भी function पर आसानी से apply किया जा सकता है
  • Logging, Timing, Authentication जैसे common use cases — ये सब decorator से आसानी से संभव होते हैं

Function को Wrap करना

Decorator के पीछे का मुख्य idea है — एक function को किसी दूसरे function के अंदर "wrap" कर देना, ताकि original function चलने से पहले और बाद में extra code चल सके।

Example — बिना @ syntax के, सिर्फ wrapping
def simple_function(): print("Main function चल रहा है") def decorator(func): def wrapper(): print("Wrap होने से पहले") func() print("Wrap होने के बाद") return wrapper decorated = decorator(simple_function) decorated()

Output:

Wrap होने से पहले Main function चल रहा है Wrap होने के बाद

यहाँ simple_function को decorator() में pass किया गया, जिसने उसे wrapper() के अंदर wrap कर दिया।

Try it Yourself »

Wrapper Function क्या है?

Wrapper Function वह inner function है जो decorator के अंदर define होता है और original function को call करने के साथ-साथ extra code भी चलाता है।

Example
def decorator(func): def wrapper(): # यह Wrapper Function है print("Extra Before") func() print("Extra After") return wrapper

wrapper() ही वह जगह है जहाँ original function (func) call होता है, और उसके इर्द-गिर्द extra behavior जोड़ा जाता है।

@decorator Syntax

Function को manually decorator(function_name) लिखकर wrap करने की बजाय, Python एक shortcut syntax देता है — @decorator_name, जिसे function के ठीक ऊपर लिखा जाता है।

बिना @ Syntax
def greet(): print("Hello!") greet = my_decorator(greet) greet()
@ Syntax के साथ — same result
@my_decorator def greet(): print("Hello!") greet()

@my_decorator लिखना बिल्कुल greet = my_decorator(greet) लिखने जैसा ही है — बस पढ़ने और लिखने में आसान है।

Try it Yourself »
Note: @decorator_name हमेशा function definition के ठीक ऊपर, बिना किसी blank line के लिखा जाता है।

Decorator का Step-by-Step Execution Flow

Example
def my_decorator(func): def wrapper(): print("Step 1: Before") func() print("Step 3: After") return wrapper @my_decorator def greet(): print("Step 2: Original Function") greet()

Output:

Step 1: Before Step 2: Original Function Step 3: After

Execution flow कुछ इस तरह चलता है:

greet() call हुआ ↓ असल में wrapper() call होता है ↓ "Before" print होता है ↓ func() यानी original greet() चलता है ↓ "After" print होता है
Try it Yourself »

Function से पहले Extra Code चलाना

Example — Login Check
def check_login(func): def wrapper(): print("User login check हो रहा है...") func() return wrapper @check_login def dashboard(): print("Dashboard खुल गया") dashboard()

Output:

User login check हो रहा है... Dashboard खुल गया
Try it Yourself »

Function के बाद Extra Code चलाना

Example — Completion Message
def notify_completion(func): def wrapper(): func() print("Task पूरा हो गया!") return wrapper @notify_completion def process_order(): print("Order process हो रहा है") process_order()

Output:

Order process हो रहा है Task पूरा हो गया!
Try it Yourself »

Original Function को बिना बदले Extra Behavior जोड़ना

Decorator की सबसे बड़ी खूबी यही है कि original function का code बिल्कुल भी नहीं बदलता — सारी extra functionality decorator के अंदर रहती है।

Example
def add_border(func): def wrapper(): print("------------------") func() print("------------------") return wrapper @add_border def show_message(): print("Welcome to PythonHindii!") show_message()

Output:

------------------ Welcome to PythonHindii! ------------------

show_message() का असली code बिल्कुल वैसा ही है — सिर्फ decorator की मदद से उसके चारों तरफ border जोड़ दिया गया।

Try it Yourself »

Practical Example — Execution Time Measure करना

Example
import time def measure_time(func): def wrapper(): start = time.time() func() end = time.time() print("Time taken:", end - start, "seconds") return wrapper @measure_time def slow_function(): for i in range(1000000): pass print("Function complete हुआ") slow_function()

Output:

Function complete हुआ Time taken: 0.0123456 seconds

यह एक बहुत common real-world use case है — बिना slow_function() का code बदले, उसका execution time measure किया जा रहा है।

Try it Yourself »

Practical Example — Arguments वाले Function पर Decorator

अगर original function arguments लेता है, तो wrapper function को भी वही arguments accept करने होंगे।

Example
def my_decorator(func): def wrapper(*args, **kwargs): print("Function चलने वाला है...") result = func(*args, **kwargs) print("Function complete हुआ") return result return wrapper @my_decorator def add(a, b): return a + b print(add(10, 5))

Output:

Function चलने वाला है... Function complete हुआ 15

यहाँ *args और **kwargs का इस्तेमाल किया गया, ताकि decorator किसी भी number of arguments वाले function पर काम कर सके।

Try it Yourself »

Common Decorator Mistakes

❌ Wrapper Function से Function को Return न करना
def my_decorator(func): def wrapper(): func() # return wrapper लिखना भूल गए @my_decorator def greet(): print("Hello") greet() # TypeError: NoneType object is not callable
✅ सही तरीका
def my_decorator(func): def wrapper(): func() return wrapper
❌ Arguments वाले Function पर बिना *args/**kwargs के Decorator लगाना
def my_decorator(func): def wrapper(): # arguments accept नहीं कर रहा func() return wrapper @my_decorator def add(a, b): return a + b add(10, 5) # TypeError
✅ सही तरीका
def wrapper(*args, **kwargs): return func(*args, **kwargs)
❌ Original Function को Call करना भूल जाना
def my_decorator(func): def wrapper(): print("Before") # func() call ही नहीं किया return wrapper

इससे original function का असली काम कभी execute ही नहीं होगा।

❌ @decorator को गलत जगह लिखना
def greet(): print("Hello") @my_decorator # function के ऊपर होना चाहिए, नीचे नहीं
✅ सही तरीका
@my_decorator def greet(): print("Hello")

Exercise ?

Decorator के अंदर बना inner function, जो original function को call करता है, उसे क्या कहते हैं?

Outer Function
Wrapper Function
Lambda Function

Coding Challenge

अब खुद अपने Online Python Editor में यह challenge solve कीजिए:

  1. एक decorator बनाइए जो किसी function के चलने से पहले "Starting..." और बाद में "Finished!" print करे।
  2. एक decorator बनाइए जो किसी function को सिर्फ 3 बार repeat करके चलाए।
  3. एक decorator बनाइए जो function का नाम print करे, फिर उसे call करे — यानी log करे कि कौन सा function चल रहा है।
  4. एक divide(a, b) function बनाइए और उस पर एक decorator लगाइए जो check करे कि b zero तो नहीं है, और अगर zero हो तो error message print करके function को call न करे।
  5. पिछले measure_time decorator को इस्तेमाल करके किसी loop वाले function का execution time निकालिए।

Quick Revision

ConceptMeaning
Decoratorकिसी function में बिना उसका code बदले extra behavior जोड़ने वाला function
Wrapper FunctionDecorator के अंदर का inner function, जो original function को call करता है
@decorator_nameDecorator apply करने का shortcut syntax
*args, **kwargsWrapper को किसी भी number of arguments accept करने देने के लिए

FAQ

Python में Decorator क्या है?

Decorator एक ऐसा function है जो किसी दूसरे function को argument की तरह लेकर उसमें extra functionality जोड़ता है, बिना original function का code बदले।

@decorator_name syntax का क्या मतलब है?

यह function = decorator_name(function) लिखने का shortcut है — इसे function definition के ठीक ऊपर लिखा जाता है।

Wrapper Function क्यों जरूरी है?

Wrapper function वह जगह है जहाँ original function call होता है और उसके इर्द-गिर्द extra code (पहले और बाद में) चलाया जाता है। Decorator को हमेशा wrapper function return करना चाहिए।

Decorator में *args और **kwargs का इस्तेमाल क्यों होता है?

ताकि decorator किसी भी number of arguments लेने वाले function पर काम कर सके — बिना यह जाने कि उस function को कितने arguments चाहिए।

Decorator कहाँ इस्तेमाल होते हैं?

Logging, execution time measure करना, authentication check करना, caching, और किसी function को बार-बार run कराने जैसी situations में decorators बहुत common हैं।

Summary

Decorator = किसी function को बिना बदले, उसमें नई क्षमता जोड़ने का तरीका।

इस article में आपने सीखा कि Decorator क्या है, इसकी जरूरत क्यों पड़ती है, function को wrap करने का concept, Wrapper Function क्या होता है, @decorator syntax कैसे काम करता है, और decorator का execution flow कैसे चलता है। साथ ही, execution time measure करने और arguments वाले functions पर decorator लगाने जैसे practical examples भी देखे।

Decorators Python के सबसे powerful concepts में से एक हैं और real-world projects में logging, authentication, caching जैसी चीज़ों में बहुत इस्तेमाल होते हैं। अब ऊपर दिया गया coding challenge खुद अपने Online Editor में solve करें।

Related Articles

🧑‍💻 Python Coding Lab Python code likho, run karo aur practice karo — directly browser mein.