Theory
ResultDesk एक file से बड़ा हो जाता है
ResultDesk का Python side आज रात शुरू होता है, और पहला temptation तुरंत दिखता है: आपको एक statistics formula के लिए square root चाहिए। अपना ख़ुद का square-root function लिखें?
किसी ने पहले ही यह किया, बीस साल test किया, और इसे Python के अंदर ship किया: math.sqrt।
जो mechanism आपको उनका काम थमाता है, और आपको अपने बढ़ते project को साफ़-सुथरी files में split करने देता है, वह module है। एक concept, और अचानक Python एक लंबी script होना बंद कर देता है।
Theory
Shelf पर toolboxes
एक workshop हर tool को एक table पर नहीं ढेर करता। Tools labelled toolboxes में रहते हैं: electrical box, plumbing box।
एक module एक toolbox है: math sqrt और pi रखता है, random randint रखता है, sqlite3 database वाली हर चीज़ रखता है। import math box को आपके bench पर उठाता है; math.sqrt इसे खोलता है और एक tool लेता है। आपकी अपनी .py files भी toolboxes हैं, जिस पल कोई दूसरी file उन्हें import करती है।
Theory
Module, formally
एक module एक file है जिसमें Python definitions और statements हैं: functions, classes, variables। .py हटाने पर filename module का नाम है।
Modularity marks क्यों कमाती है:
- Reusability: एक बार लिखिए, हर जगह import कीजिए।
- Organization: ResultDesk db.py, reports.py, charts.py में बँटता है।
- Namespace separation: math.sqrt और आपका अपना sqrt साथ रह सकते हैं, हर एक अपने module के नाम के पीछे।
Python की standard library सैकड़ों pre-installed modules है: math, random, datetime, csv, sqlite3।
Practical
तीन import forms (plus एक जिससे बचना है)
# Form 1: import the whole toolbox, use with the prefix
import math
print(math.sqrt(49)) # 7.0
# Form 2: take one tool out, use it bare
from math import sqrt
print(sqrt(49)) # 7.0 (no math. prefix now)
# Form 3: alias a long name
import sqlite3 as sq
conn = sq.connect('college.db')
# Your own module: save this as grades.py ...
# def grade(score):
# return 'Pass' if score >= 40 else 'Fail'
# ... then in another file in the same folder:
# import grades
# print(grades.grade(78)) # Pass
# Avoid: from math import * (floods your namespace)
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
एक file चलती है: from math import sqrt। बाद में कौन सी call सही है?
- sqrt(25): नाम bare import हुआ, math.sqrt को import math चाहिए होता
- math.sqrt(25): from-import अभी भी prefix चाहता है
- sqrt(25) और math.sqrt(25) दोनों हमेशा काम करते हैं
- कोई नहीं: from-import सिर्फ़ functions के अंदर काम करता है
Show the answer
sqrt(25): नाम bare import हुआ, math.sqrt को import math चाहिए होता
from math import sqrt अकेला नाम sqrt आपकी file के namespace में copy करता है: इसे bare call कीजिए। नाम math ख़ुद कभी import नहीं हुआ, तो math.sqrt NameError उठाता है, option B और C को मार देते हुए। Import form को call syntax से match करना सबसे ज़्यादा tested module fact है: import m देता है m.f(); from m import f देता है f()।
Think first
Shadowing mystery
एक student अपनी Unit 4 practice file csv.py के रूप में save करता है, और इसके अंदर Python के CSV toolbox का इस्तेमाल करने के लिए import csv लिखता है। हर run AttributeError: module 'csv' has no attribute 'reader' से crash होता है। tap करने से पहले: Python असल में क्या import कर रहा है?
Show the answer
उनकी अपनी file। Python पहले current folder search करता है, csv.py (student की) पाता है, और standard library के csv के बजाय इसे import करता है: जिसमें ज़ाहिर है कोई reader नहीं है। File ने असली module को shadow कर दिया है।
Fix: file rename कीजिए (csv_practice.py)। Rule: अपनी file को कभी उस module के नाम पर मत रखिए जिसे आप import करते हैं। यह bug beginners के घंटे बर्बाद करता है ठीक इसलिए क्योंकि code perfect दिखता है।
Watch out
दो आदतें जो दर्द रोकती हैं
Star imports skip कीजिए: from math import * दर्जनों नाम आपकी file में dump कर देता है; दो modules बाद, नाम collide करते हैं और debugging archaeology बन जाती है। जो नाम आप लेते हैं उसे import कीजिए।
Module code import पर एक बार चलता है: किसी module के top-level पर कोई भी print पहली बार कोई इसे import करने पर fire करता है (फिर cached है)। Modules को definitions तक रखिए; actions importing script में रखिए।
Theory
यह unit एक लंबा import है
इस subject में आगे सब कुछ अलग hat पहने एक module है: sqlite3 (अगला lesson: Python से college.db से connect करना), csv (Unit 4), pandas और numpy (universal convention से pd और np के रूप में aliased), matplotlib.pyplot as plt (Unit 5)। आज तीनों import forms master कीजिए और बाक़ी हर lesson एक line तेज़ शुरू होता है।
Summary
Key takeaways
- एक module = definitions की एक .py file; filename module name है।
- import m → m.f() call कीजिए; from m import f → f() call कीजिए; import m as alias → alias.f()।
- from m import * से बचिए: namespace flooding और collisions।
- Standard library math, random, datetime, csv, sqlite3 और सैकड़ों और pre-install करती है।
- आपकी अपनी .py files उसी folder से उसी तरीक़े से import होती हैं।
- किसी file को कभी उस module के नाम पर मत रखिए जिसे आप import करते हैं: यह असली को shadow करता है।
- Memory hook: shelf पर toolboxes।