Python Modules

Python Modules

There are actually three different ways to define a module in Python.

पायथन में मॉड्यूल को परिभाषित करने के लिए वास्तव में तीन अलग-अलग तरीके हैं।

  • A module can be written in Python itself.

पायथन में ही एक मॉड्यूल लिखा जा सकता है।

  • A module can be written in C and loaded dynamically at run-time, like the regular expression module. एक मॉड्यूल C में लिखा जा सकता है और नियमित एक्सप्रेशन मॉड्यूल की तरह, रन-टाइम पर गतिशील रूप से लोड किया जा सकता है।

  • A built-in module is fundamentally contained in the interpreter.

एक बिल्ट-ईन मॉड्यूल इंटरप्रेटर में मौलिक रूप से निहित होता है।

A module’s contents are accessed the same way in all three cases: with the import statement.

मॉड्यूल की सामग्री को सभी तीन मामलों में उसी तरह एक्सेस किया जाता है: इंपोर्ट स्टेटमेंट के साथ।

Create a module – एक मॉड्यूल बनाएं

Type the following and save it as example.py.
निम्नलिखित टाइप करें और इसे example.py के रूप में सेव करें।

var = 1979
def add (a, b) :
    print ("The Sum : ", a+b)
def mul (a, b) :
    print ("The Multiplication : ", a*b)

example.py module contains one variable and two functions.
example.py मॉड्यूल में एक वैरियेबल और दो फंक्शन हैं।

Import modules – इंपोर्ट मॉड्यूल्स

We can import the definitions inside a module to another module or the interactive interpreter in Python. We use the import keyword to do this. To import our previously defined module example we type the following in the Python prompt.

हम एक मॉड्यूल के अंदर परिभाषाओं को दूसरे मॉड्यूल या पायथन में इंटरएक्टिव इंटरप्रेटर इंपोर्ट कर सकते हैं। हम ऐसा करने के लिए इंपोर्ट कीवर्ड का उपयोग करते हैं। अपने पहले से परिभाषित मॉड्यूल उदाहरण को इंपोर्ट करने के लिए हम पायथन प्रॉम्प्ट में निम्नलिखित टाइप करते हैं।

import example

This does not enter the names of the functions defined in example directly in the current symbol table. It only enters the module name example there. Using the module name we can access the function using the dot’:’ operator.

यह उदाहरण में परिभाषित फंक्शन के नामों को सीधे वर्तमान सिम्बल टेबल में दर्ज नहीं करता है। यह केवल मॉड्यूल नाम उदाहरण में प्रवेश करता है। मॉड्यूल नाम का उपयोग करके हम डॉट .’ ऑपरेटर का उपयोग करके फंक्शन तक पहुंच सकते हैं।

>>> add (4,5)
9
>>> mul (4,5)
20
>>> print (var)
1979

Note: Whenever we are using a module in our program, for that module compiled file will be generated and stored in the hard disk permanently.

जब भी हम अपने प्रोग्राम में एक मॉड्यूल का उपयोग कर रहे हैं, तो उस मॉड्यूल के लिए संकलित फाइल को हार्ड डिस्क में स्थायी रूप से उत्पन्न और संग्रहीत किया जाएगा।

Renaming a module at time of import (module aliasing) –
इंपोर्ट के समय एक मॉड्यूल का नाम बदलना (मॉड्यूल अलियासिंग)

You can also import an entire module under an alternate name:

आप वैकल्पिक नाम के तहत एक संपूर्ण मॉड्यूल भी इंपोर्ट कर सकते हैं:

Syntax: import <moduleName (s) > as <aliasing>

import example as e

Here, example is orginal module name and ‘e’ is alias name. we can access members by using alias name ‘e’.

यहाँ, उदाहरण ओरिजनल मॉड्यूल नाम है और ‘e’ उपनाम नाम है। हम उपनाम नाम ‘e’ का उपयोग करके मेम्बर्स तक पहुँच सकते हैं।

Example:
>>>import example as e
>>>print (e.var)
1979
>>>e.add (2,2)
The Sum :  4
>>>e.mul (2,2)
The Multiplication :  4

1.from … import – इंपोर्ट से…..

We can import particular members of module by using from … import. Then main advantage of this concept is we can access members directly without using module.

हम from … import मॉड्यूल के विशेष सदस्यों को इंपोर्ट कर सकते हैं। तब इस अवधारणा का मुख्य लाभ यह है कि हममॉड्यूल का उपयोग किए बिना सीधे सदस्यों तक पहुंच सकते हैं।

Syntax: from <module_name> import <name (s) >

Example:
>>>from example import var, add, mul
>>>print (var)
1979
>>>add (200, 300)
The Sum :  500
>>>mul (21, 34)
The Multiplication :  714

We can import all members of a module as follow. …
हम फॉलो के रूप में एक मॉड्यूल के सभी सदस्यों को इंपोर्ट कर सकते हैं….

>>>from example import *
>>>print (var)
1979
>>>add (200, 300)
The Sum :  500
>>>mul (21, 34)
The Multiplication :  714

Various possibilities of import statement
इंपोर्ट स्टेटमेंट की विभिन्न संभावनाएं

import modulename
import modulel, module2, module3
import module as aliasname
import modulel as mi, module2 as m2
from module import member
from module import member1, member2, member3
from module import memberi as aliasname
from module import *

Finding members of module by using dir() function
dir() फंक्शन का उपयोग करके मॉड्यूल के सदस्यों का पता लगाना

Python provides built-in function dir() to list out all members of current module or a specified module.

पायथन वर्तमान मॉड्यूल या एक निर्दिष्ट मॉड्यूल के सभी सदस्यों को सूचीबद्ध करने के लिए बिल्ट-इन फंक्शन dir() प्रदान करता है।

dir()    #To list out all members of current module
dir (modulename)  # To list out all members of specified module

Example 1: To print all members of current module, and importing module.
उदाहरण 1: करेंट मॉड्यूल के सभी सदस्यों को मुद्रित करने के लिए, और इंपोर्टिंग मॉड्यूल ।

import example 
x = 100
y = 200 
def fun1 () :
    print("Fun()")
def fun2 (a, b):
    print (a+b)
print ("current module list : \n", dir())
print ("example module list : In", dir (example))

Output:
current module list :
 [‘__annotations__’, ‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘example’, ‘fun1’, ‘fun2’, ‘x’, ‘y’]
example module list : In [‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘add’, ‘mul’, ‘var’]

Note: For every module at time of execution Python interpreter will add some special properties automatically for internal use.

नोटः निष्पादन के समय प्रत्येक मॉड्यूल के लिए पायथन इंटरप्रेटर आंतरिक उपयोग के लिए स्वचालित रूप से कुछ विशेष प्रॉपर्टीज जोड़ देगा।

[‘_builtins__’, ‘__cached_’, ‘__doc__’, ‘_file__’, ‘_loader__’,
__name__’, ‘_package__’, ‘_spec__’,

Based on our requirement we can also use or access these properties in out program.

अपनी आवश्यकता के आधार पर हम इन प्रॉपर्टीज का उपयोग कर सकते हैं या बाहर प्रोग्राम में पहुँच सकते हैं।

Example:
print(__builtins__)
print (__cached__)
print (__doc__)
print(__file__ )
print(__loader__)
print(__name__)
print (__package__)
print (__spec__)

Chapter wise Model Paper Link

About Me