Python Module Overview

Python me Module ka concept bilkul usi tarah hai jaisa dusre programming languages me Library ka concept hota hai. Module ke andar functions/classes/variables define kiye ja sakte hain jise requirement ke dauran python me import kar liya jata hai.

Python ke case me almost sare works modules ke through hote hai. Modules code ko bahut hi short aur user friendly bana dete hain.  

Module kaise banaya jata hai?

python me modules ko likhne ka concept same waisa hi hai jaise aap python me coding karte hain, means modules ko bhi ‘.py’ extension ke sath save kiya hai. Example se samajhte hain : 

#Module File name – firstModule.py

def intro(word):
    print(“Getting started with, ” + word)

location = {
    “city”:”indore”
    “state”: “Madhya Pradesh”,
    “country”: “India”
}

Module ko import kaise kiya jata hai?

jaisa ki humne ‘firstModule.py’ naam ka ek module banaya hai, ab hum chahte hain ki iss module ko import karen.

Example se samajhte hain: 

#Now we are calling firstModule.py in our code

import firstModule
firstModule.intro(“Python”)

City = firstModule.location([“city”]) 
State = firstModule.location([“state”])  
Country =
 firstModule.location([“country”])

print(City)
print(State)
print(Country)

*** Module ko import karte time uske location par bhi dhyan dena hota hai, agar module project ke root folder me available hai to use direct (import <module_name>) call kiya ja sakta hai aur agar wahi module kisi dusre location me hai to uske exact path ko locate karke usko call karna hota hai.

Module ka Alias Naam

Alias means copy…matlab 1 naam ke kai naam lekin properties poori same. Example se samjhe to code me  firstModule ko uska alias naam dekar use short kiya ja sakta hai aur usko firstModule ki tarah code me finally use bhi kiya ja sakta hai. 

#firstModule == fm

import firstModule as fm
fm.intro(“Python”)

City = fm.location([“city”]) 
State = fm.location([“state”])  
Country =
 fm.location([“country”])

print(City)
print(State)
print(Country)

Python me Import From Module ka concept

Agar aap poore module ko call nhi karna chahte lekin uss module ke kisi particular function/class/variable ko access karna chahte hain to aap “import from module” ka logic apply kar sakte hain. jaise upar diye gaye code me aap “firstModule” ka use karke “state” ki value print karna chahte hain. Example se dekhte hain :  

#import from module logic

from firstModule import location
State = firstModule.location([“state”])  
print(State)