Modules and Packages
Modules aur Packages sunne mai kafi fancy term lag raha hai, lekin is post ko padhne ke baad apko clear hojayega ki modules aur packages kya hai ?
let's suppose apne ek python program banaya aur usse save kr diya rishabh.py name se, aur fir apne ek dusra program banaya aur ussse save kr diya gupta.py name se , to basically rishabh.py ek module ho gya aur ussi tarah gupta.py bhi ek module ho gya aur har module ke andar kuch classes hongi kuch , kuch functions honge , aur ham in classes or function ka use kr skte hai dusre program mai import statement ki madad se jo ki hm aange dekhne wale hai , to basically module .py name se save koi bhi python program hogaya, to ab baat krte hai ki packages kya hote hai , to packages basically collection of modules and empty __init__.py file hote hai, to agar upar wala example le to agar hm rishabh.py , gupta.py aur empty __init__.py file ko ek folder mai rakhe to vo folder ek Package kahelayega.
module - rishabh.py, gupta.py etc.
package - rishabh.py + gupta.py +empty __init__.py file
Python mai abhi tak aprox 130k+ Packages hai.
Hm in packages aur modules ka use apne program mai kr skte hai by using import statement , aur ham khud ke modules aur package bna skte hai
Let's take an example
Hamne ek program banaya
---------------------------------------------------------------------------------
def func_in_mymodule():
print("I am a function inside of the mymodule.py file!")
--------------------------------------------------------------------------------
aur is program ko hmne mymodule.py file se save kr liya aur ham isksa use dusra program mai kai tarike se kr skte hai, jaise....
---------------------------------------------------------------------------------
#Tarika - 1
import mymodule
mymodule.func_in_mymodule()
----------------------------------------------------------------------------------
#Tarika - 2
import mymodule as mm
mm.func_in_mymodule()
----------------------------------------------------------------------------------
#Tarika - 3
from mymodule import func_in_mymodule
func_in_mymodule()
--------------------------------------------------------------------------------
#Tarika - 4 (worst tarika)
from mymodule import *
func_in_mymodule()
#Ye sbse kharab tarika hai, * ki madad hm saare classes aur functions ko import kr lete hai , ye basically code readabiliy ko kam kr deta hai, isliye ye sbse ghatiya tarika mana jata hai.
-----------------------------------------------------------------------
Comments
Post a Comment