Home >> Python Tutorial >> Nested Function
Edit Template
Edit Template

Nested Function in Hindi

What is Nested Function

जब हम किसी function के अंदर दूसरे function को define करते है तो उसे nested function कहा जाता है।

🐍
Example 1.py
Copy to clipboard
def outer():
    print("This is outer function")
    def inner():
        print("This is inner function")
    inner()

outer()
''' Output:
This is outer function
This is inner function
'''

Return Function from Function

Python में कोई function किसी दूसरे function को return कर सकता है।

🐍
Example 2.py
Copy to clipboard
def outer():
    def inner():
        return "Hello from inner"
    return inner   # inner function ko return kiya

func = outer()
print(func())   # inner function call

# Output: Hello from inner

Higher Order Function

ऐसा function जो किसी function को argument के रूप में ले और किसी function को return करे तो ऐसे function को Higher Order Function कहेंगे।

🐍
Example 3.py
Copy to clipboard
def square(x):
    return x * x

def apply_func(func, value):   # function as parameter
    return func(value)

print(apply_func(square, 5))

# Output: 25

Function Recursion

Function मे recursion तब होता है जब हम किसी function मे ही उसी function को call करते है 

SYNTEX

def function_name()

       function_name()

🐍
Example 4.py
Copy to clipboard
def factorial(n):
  if n == 0 or n == 1:
    return 1
  else:
    return n * factorial(n - 1)
num = 5
result = factorial(num)
print(f"The factorial of {num} is: {result}")
# Output: The factorial of 5 is: 120

Closure

जब एक nested function अपने enclosing function (बाहर वाला function) के variables को याद करता है जबकि enclosing function execute हो चुका हो, तो उसे Closure कहते है।

🐍
Example 5.py
Copy to clipboard
def outer(msg):
    def inner():
        print("Message:", msg)   # msg ko yaad rakha
    return inner

say_hello = outer("Hello Python")
say_hello()   # closure ne msg ko yaad rakha

# Output: Message: Hello Python

Decorator

Decorator एक ऐसा nested function है जो किसी दूसरे function के behavior को change या extend कर है, बिना उस function के code को बदले।

Decorators का use हम तब करते है जब हमे किसी function में extra functionality add करना हो, या बिना उस function के code को modify किए उसमे कोई नया behavior inject करना हो।

किसी function पर decorator को manually apply कर सकते है but अक्सर @decorator syntax किया जाता है इस syntax में हमे जिस भी function पर जो decorator apply करना हो हम simply उस function के ऊपर “@” के साथ उस decorator के name (@decorator_name) को लिख देता है। 

हम जिस function पर decorator को apply करना चाहते है अगर वो function कोई arguments लेता है तो wrapper (decorator के अंदर वाला function) में “*args” और “**kwargs” लिखना जरूरी होता है।

🐍
Example 6.py
Copy to clipboard
def decorator_function(original_function):
    def wrapper():
        print("Before function")
        original_function()
        print("After function")
    return wrapper

# Apply decorator manually
def say_hello():
    print("Hello Python")
decorated = decorator_function(say_hello)
decorated()

''' Output:
Before function
Hello Python
After function'''

# Using @decorator Syntax
@decorator_function
def say_hello():
    print("Hello Python")

say_hello()

''' Output:
Before function
Hello Python
After function'''

उम्मीद करते है कि आपको Nested Function अच्छे समझ मे आ गया होगा। अपने learning को continue रखने के लिए next button पर click करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
Dunder Methods

Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods   Python Tutorial Python Introduction Identation &...

Encapsulation

Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers      2.1. Public Members      2.2. Protected Members...

Lambda Function

Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...

Nested Function

Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...

Context Manager

Table of Contents All Chapters 1. Context Manager 2. with Statement      2.1. __enter__() Method      2.2. __exit__()...

File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Edit Template
Scroll to Top