Skip to main content

Python Modules

Python Modules

Here we will be discussing about the highest level program organization unit, which helps programmers to pack and organize program code and data for reusability purpose.

What is Modular Programming?

Module provides a simple way to organize program components into a system; grouping similar forms of code into a module makes code easier to understand. It is also referred to as program object that binds data and can be used as reference. In other words, it is file that contains Python program. Programmers use the ‘import’ statement to get access to the names in a module’s global scope. We will learn about it a bit later. Python’s module ultimately allows programmers to link individual files to a larger program system.

Roles Of Python Module

  • Code Reuse: Modules let programmers save source code in files permanently & hence the codes in module files are persistent. Module codes can rerun as many times as required.
  • System Namespace Partitioning: In Python, the highest-level organizational unit of code is the Modules. Although they are just packages with names – these packages are self-contained. Like that of a local scope of functions, modules help to avoid name clashes across the program. So in a module, all the objects and codes are implicitly enclosed or bounded. Hence, modules are the natural programming tools used to group system components.
  • Shared Service or data Implementation: From the operational point of view, modules are also used to implement components that are shared across systems. For example, if a programmer needs to provide a global object which will be used by more than one files or function blocks, then the code can be used as a module that can be imported by several files.

import Statement

Programmers can use any Python source code (i.e. .py files) as a module by implementing an ‘import’ statement in another Python program or Python file.
Let us take a scenario, a file name ‘g.py’ be chosen as the top-level file, it will be simple text file of statements & will be executed from top to bottom as the interpreters do. The modules are – ‘a.py’ and ‘b.py’. So to use the codes of a.py and b.py in g.py program, we have to first define the codes as functions in both modules & then import the two Python codes using ‘import’ statement.
The code will be:
#!/usr/bin/python

def karl(text) #a.py File
print(text, 'karl')
Now, the programmer wants to use the codes of a.py into g.py file.
So the program will be:
Example:
#!/usr/bin/python
import a #g.py file
a
.karl ('myself') #Prints "myself karl"
The figure below shows its working: 
So the syntax for import statement is:
Example:
#!/usr/bin/python
import module_name1 [, module_name2 [module_nameN]]
The module can be imported only when the interpreter encounters an import statement & if the module is present in the search path, i.e. the directories that the interpreter searches before importing module.

Standard Library Modules In Python

Python comes with a huge collection of regular used utility modules called standard library; which contains a collection of over two-hundred modules along with platform-independent support system for common programming tasks such as network & internet scripting, text pattern matching, object persistence, GUI (Graphical User Interface) programs and much more.
Though these tools are not the part of Python programming language, but programmers can use them by importing the appropriate modules based on the needs. Since they are standard library modules, therefore they will be available & will work portably on most platforms on which the Python will be run.

How import Works?

Because ‘import'(s) are the most important part of Python program structure, let’s go to the inside of its working technique in brief. From the C-programmer’s view point – import operation is like #include of C Language but in reality ‘import’ is not just textual insertion of one file into another. These are runtime operational statements that perform three distinct steps. These are:
  • Find the module’s file from the search-path
  • Compile it to byte-code (if needed)
  • To build the object, run the module’s code
It has to be kept in mind, that the above three steps are performed only the first time the module is imported during a program execution. If the program is run later importing the previous modules, bypass all these three steps & simply fetch the already loaded module-object in memory.

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...