
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
जब हम किसी function के अंदर दूसरे function को define करते है तो उसे nested function कहा जाता है।
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
'''
Python में कोई function किसी दूसरे function को return कर सकता है।
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
ऐसा function जो किसी function को argument के रूप में ले और किसी function को return करे तो ऐसे function को Higher Order Function कहेंगे।
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 मे ही उसी function को call करते है
SYNTEX
def function_name()
function_name()
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
जब एक nested function अपने enclosing function (बाहर वाला function) के variables को याद करता है जबकि enclosing function execute हो चुका हो, तो उसे Closure कहते है।
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 एक ऐसा 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” लिखना जरूरी होता है।
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 करे,
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers 2.1. Public Members 2.2. Protected Members...
Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...
Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...
Table of Contents All Chapters 1. Parameters and Aguments 2. Function Parameters 2.1. *args 2.2. **kwargs...
Table of Contents All Chapters 1. Context Manager 2. with Statement 2.1. __enter__() Method 2.2. __exit__()...
Table of Contents All Chapters 1. Error Handling 2. Types of Error 2.1. Syntax Error 2.2....
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...