Module: concepts of module and using modules in Python

Python નું module એટલે ખાલી એક .py file જેનાં functions અને variables બીજી files import દ્વારા વાપરી શકે, અને import નું વિધાન ત્રણ સ્વરૂપે આવે છે જે ચોકસાઈથી જાણવા જેવાં છે.

8 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

ResultDesk એક file થી મોટું થાય છે

ResultDesk ની Python ની બાજુ આજ રાતથી શરૂ થાય છે, અને પહેલી લાલચ તરત જ દેખાય છે: તમને આંકડાશાસ્ત્રના સૂત્ર માટે વર્ગમૂળ જોઈએ છે. તમારું પોતાનું વર્ગમૂળનું function લખશો?

કોઈએ એ પહેલેથી લખ્યું છે, વીસ વર્ષ સુધી ચકાસ્યું છે, અને Python ની અંદર મોકલ્યું છે: math.sqrt.

એ પદ્ધતિ જે તમને એમનું કામ સોંપે છે, અને તમારા વધતા project ને સુઘડ files માં વહેંચવા દે છે, એ છે module. એક ખ્યાલ, અને અચાનક Python એક લાંબી script મટી જાય છે.

Theory

છાજલી પરની ઓજારપેટીઓ

કારખાનું દરેક ઓજાર એક જ મેજ પર ઢગલો કરતું નથી. ઓજારો લેબલવાળી ઓજારપેટીઓ માં વસે છે: વીજળીની પેટી, પ્લમ્બિંગની પેટી.

Module એ ઓજારપેટી છે: math માં sqrt અને pi છે, random માં randint છે, sqlite3 માં database નું બધું છે. import math પેટીને તમારા મેજ પર ઊંચકે છે; math.sqrt એ ખોલીને એક ઓજાર લે છે. તમારી પોતાની .py files પણ ઓજારપેટીઓ છે, જે ક્ષણે બીજી file એમને import કરે.

Theory

Module, ઔપચારિક રીતે

Module એટલે Python ની વ્યાખ્યાઓ અને વિધાનો ધરાવતી file: functions, classes, variables. File નું નામ .py બાદ કરતાં એ module નું નામ છે.

Modularity પરીક્ષામાં marks કેમ કમાય છે:

  • ફરી વાપરી શકાય: એક વાર લખો, બધે import કરો.
  • વ્યવસ્થા: ResultDesk db.py, reports.py, charts.py માં વહેંચાય છે.
  • Namespace નું જુદાપણું: math.sqrt અને તમારું પોતાનું sqrt સાથે રહી શકે છે, દરેક પોતાના module ના નામ પાછળ.

Python ની standard library સેંકડો પહેલેથી install થયેલાં modules છે: math, random, datetime, csv, sqlite3.

Practical

Import નાં ત્રણ સ્વરૂપ (વત્તા એક ટાળવા જેવું)

# 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 સાચું છે?

  1. sqrt(25): નામ ખુલ્લું import થયું, math.sqrt માટે import math જોઈએ
  2. math.sqrt(25): from-import ને હજી prefix જોઈએ
  3. sqrt(25) અને math.sqrt(25) બંને હંમેશા ચાલે છે
  4. એકેય નહીં: from-import ફક્ત functions ની અંદર ચાલે છે
Show the answer

sqrt(25): નામ ખુલ્લું import થયું, math.sqrt માટે import math જોઈએ

from math import sqrt એ ફક્ત sqrt નામ તમારી file ના namespace માં નકલ કરે છે: એને ખુલ્લું બોલાવો. math નામ પોતે ક્યારેય import થયું જ નહોતું, એટલે math.sqrt એ NameError આપે છે, જે વિકલ્પ B અને C ને મારી નાખે છે. Import ના સ્વરૂપને call ની syntax સાથે મેળવવું એ module ની સૌથી વધુ ચકાસાતી હકીકત છે: import m એ m.f() આપે છે; from m import f એ f() આપે છે.

Think first

ઢાંકી દેવાનું રહસ્ય

એક student પોતાની એકમ 4 ની પ્રેક્ટિસની file csv.py તરીકે સાચવે છે, અને એની અંદર Python ની CSV ની ઓજારપેટી વાપરવા import csv લખે છે. દરેક વખતે AttributeError: module 'csv' has no attribute 'reader' થી ભાંગી પડે છે. Tap કરતાં પહેલાં: Python ખરેખર શું import કરી રહ્યું છે?

Show the answer

એમની પોતાની file. Python પહેલાં હાલનું folder શોધે છે, csv.py (student ની) મળે છે, અને standard library ના csv ને બદલે એ import કરે છે: જેમાં અલબત્ત કોઈ reader નથી. File એ ખરેખરા module ને ઢાંકી દીધું છે.

ઉકેલ: file નું નામ બદલો (csv_practice.py). નિયમ: તમે import કરો છો એ module ના નામે તમારી file ક્યારેય ન રાખો. આ bug શરૂઆત કરનારના કલાકો ખાય છે ખાસ એટલા માટે કે code સંપૂર્ણ દેખાય છે.

Watch out

પીડા અટકાવતી બે ટેવો

Star imports છોડો: from math import * તમારી file માં ડઝનબંધ નામ ઠાલવે છે; બે modules પછી, નામ અથડાય છે અને debugging પુરાતત્ત્વ બની જાય છે. જેનું નામ લો એ જ import કરો.

Module નો code import વખતે એક વાર ચાલે છે: module ના ઉપરના સ્તરે કોઈ print હોય તો કોઈ પહેલી વાર એને import કરે ત્યારે એ ચાલે છે (પછી એ cache થાય છે). Modules માં ફક્ત વ્યાખ્યાઓ રાખો; ક્રિયાઓ import કરતી script માં રાખો.

Theory

આ એકમ એક લાંબું import છે

આ વિષયમાં આગળનું બધું જુદી ટોપી પહેરેલું module છે: sqlite3 (આગલો પાઠ: Python માંથી college.db સાથે જોડાવું), csv (એકમ 4), pandas અને numpy (સાર્વત્રિક સંમેલન પ્રમાણે pd અને np તરીકે alias કરાયેલાં), matplotlib.pyplot as plt (એકમ 5). આજે import નાં ત્રણ સ્વરૂપ સિદ્ધ કરો અને બાકીનો દરેક પાઠ એક લીટી વહેલો શરૂ થાય છે.

Summary

Key takeaways

  • Module = વ્યાખ્યાઓની .py file; file નું નામ એ module નું નામ છે.
  • import m → m.f() બોલાવો; from m import f → f() બોલાવો; import m as alias → alias.f().
  • from m import * ટાળો: namespace ભરાઈ જવું અને અથડામણ.
  • Standard library math, random, datetime, csv, sqlite3 અને સેંકડો બીજાં પહેલેથી install કરે છે.
  • તમારી પોતાની .py files એ જ રીતે, એ જ folder માંથી import થાય છે.
  • તમે import કરો છો એ module ના નામે file ક્યારેય ન રાખો: એ ખરેખરાને ઢાંકી દે છે.
  • Memory hook: છાજલી પરની ઓજારપેટીઓ.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python interaction with SQLite

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati