|
प्रश्न 5. __name__ == "__main__" का उपयोग क्यों किया जाता है? |
जब Python Program छोटा होता है, तब पूरा Code एक ही File में लिखा जा सकता है। लेकिन जैसे-जैसे Program बड़ा होता जाता है, वैसे-वैसे उसे संभालना, Debug करना और Maintain करना कठिन हो जाता है।
इस समस्या का समाधान Module है।
Module की सहायता से बड़े Program को कई छोटे-छोटे भागों (Files) में विभाजित किया जा सकता है। प्रत्येक Module किसी विशेष कार्य (Specific Task) के लिए बनाया जाता है। आवश्यकता पड़ने पर इन Modules को अन्य Programs में Import करके पुनः उपयोग (Reuse) किया जा सकता है।
Python में पहले से कई Built-in Modules उपलब्ध हैं, जैसे math, random, os, datetime आदि। इसके अलावा Programmer अपनी आवश्यकता के अनुसार स्वयं भी Modules बना सकता है।
Module एक Python File (.py) होती है, जिसमें Functions, Variables, Classes तथा Executable Code लिखा जा सकता है।
सरल शब्दों में—
Module = Python Code वाली एक .py File
उदाहरण—
calculator.py
यह एक Module है।
Module का उपयोग क्यों किया जाता है?
बड़े Programs में पूरा Code एक ही File में लिखना उचित नहीं होता।
Modules का उपयोग करने से—
- Code व्यवस्थित रहता है।
- Reusability बढ़ती है।
- Debugging आसान होती है।
- Team Work सरल हो जाता है।
- Maintenance आसान हो जाती है।
Python में मुख्य रूप से दो प्रकार के Modules होते हैं—
- Built-in Module
- User-defined Module
Python के साथ पहले से उपलब्ध Modules को Built-in Modules कहते हैं।
कुछ प्रमुख Built-in Modules—
|
Module |
उपयोग |
|
math |
Mathematical Functions |
|
random |
Random Numbers |
|
os |
Operating System से संबंधित कार्य |
|
datetime |
Date और Time |
|
sys |
Python Interpreter से संबंधित जानकारी |
|
statistics |
Statistical Calculations |
|
calendar |
Calendar Operations |
जो Module Programmer स्वयं बनाता है, उसे User-defined Module कहते हैं।
उदाहरण—
def add(a, b):
return a + b
def sub(a, b):
return a - b
अब इस Module को किसी दूसरे Program में उपयोग कर सकते हैं।
Python में Module को उपयोग करने के लिए import Statement का उपयोग किया जाता है।
import module_name
import math
print(math.sqrt(25))
Output
5.0
User-defined Module Import करना
मान लें हमारे पास calculator.py नाम की File है।
import calculator
print(calculator.add(20, 10))
Output
30
यदि पूरे Module की आवश्यकता न हो, तो केवल आवश्यक Function Import किया जा सकता है।
from module_name import function_name
from math import sqrt
print(sqrt(64))
Output
8.0
Multiple Functions Import करना
from math import sqrt, factorial
print(sqrt(49))
print(factorial(5))
किसी Module के सभी Public Members Import करने के लिए—
from math import *
print(pi)
print(sqrt(16))
Best Practice: import * का उपयोग सामान्यतः नहीं करना चाहिए क्योंकि इससे Namespace में अनावश्यक Names आ जाते हैं और Code कम Readable हो जाता है।
यदि Module का नाम बड़ा हो, तो Alias बनाया जा सकता है।
import module_name as alias
import math as m
print(m.sqrt(81))
from math import factorial as fact
print(fact(6))
dir() किसी Module में उपलब्ध सभी Members की सूची दिखाता है।
import math
print(dir(math))
किसी Module या Function की Documentation देखने के लिए।
import math
help(math)
हर Python Module में __name__ नाम का एक Built-in Variable होता है।
यदि Module सीधे Run किया जाता है, तो इसकी Value __main__ होती है।
यदि Module Import किया जाता है, तो इसकी Value Module का नाम होती है।
print(__name__)
सीधे Run करने पर Output—
__main__
इसका उपयोग यह निर्धारित करने के लिए किया जाता है कि Code केवल तभी Execute हो जब File सीधे Run की जाए।
def welcome():
print("Welcome")
if __name__ == "__main__":
welcome()
यदि यह File Import होगी, तो welcome() अपने आप Execute नहीं होगा।
जब Python किसी Module को Import करता है, तो वह निम्न स्थानों पर खोजता है—
- Current Folder
- Python Standard Library
- Installed Packages (site-packages)
यदि Module नहीं मिलता, तो ModuleNotFoundError आता है।
import math
print(math.sqrt(100))
import math as m
print(m.factorial(5))
from math import pi
print(pi)
Program 4 – User-defined Module
calculator.py
def multiply(a, b):
return a * b
main.py
import calculator
print(calculator.multiply(10, 20))
print(__name__)
import abcxyz
Error
ModuleNotFoundError
import math
print(math.add())
Error
AttributeError
क्योंकि math Module में add() नाम का Function नहीं है।
- प्रत्येक Module एक विशेष कार्य के लिए बनाएँ।
- Meaningful Module Name रखें।
- import * से बचें।
- जहाँ संभव हो Alias (as) का उपयोग करें।
- User-defined Modules को छोटे और Reusable रखें।
- if __name__ == "__main__": का उपयोग Test Code के लिए करें।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- Module एक .py File होती है।
- Module में Functions, Classes और Variables हो सकते हैं।
- import Module Import करने के लिए उपयोग किया जाता है।
- from...import केवल आवश्यक Members Import करता है।
- as Alias बनाने के लिए उपयोग होता है।
- dir() Module के सभी Members दिखाता है।
- help() Documentation दिखाता है।
- __name__ Module का Built-in Variable है।
- __main__ तब मिलता है जब File सीधे Execute की जाती है।
Frequently Asked Questions (FAQs)
Python Code वाली .py File जिसमें Functions, Variables या Classes हो सकते हैं।
प्रश्न 2. Built-in Module क्या है?
जो Module Python के साथ पहले से उपलब्ध हो, जैसे math, random, os।
प्रश्न 3. User-defined Module क्या है?
जो Module Programmer स्वयं बनाता है।
प्रश्न 4. import और from...import में क्या अंतर है?
import पूरे Module को Import करता है, जबकि from...import केवल आवश्यक Members Import करता है।
प्रश्न 5. __name__ == "__main__" का उपयोग क्यों किया जाता है?
ताकि कुछ Code केवल तभी Execute हो जब File सीधे Run की जाए, Import होने पर नहीं।
- Module → .py File
- Built-in Module → Python द्वारा उपलब्ध
- User-defined Module → Programmer द्वारा बनाया गया
- import → पूरा Module Import
- from...import → Specific Member Import
- as → Alias
- dir() → Module के Members
- help() → Documentation
- __name__ → Built-in Variable
- if __name__ == "__main__": → Direct Execution Check
Python Packages in Hindi – Package क्या है, Package बनाना, __init__.py, Nested Packages, Importing Packages, Relative और Absolute Imports, Namespace Packages तथा Practical Programs। यह Python में बड़े Projects को व्यवस्थित करने का महत्वपूर्ण विषय है।