Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

MODULES AND PACKAGES Structure Introduction Objectives Module Creation and Usage Module Search Path Module Vs Script Package Creation and Importing Standard Library Modules Summary INTRODUCTION Modules are files that contain various functions, variables or classes which are log...

MODULES AND PACKAGES Structure Introduction Objectives Module Creation and Usage Module Search Path Module Vs Script Package Creation and Importing Standard Library Modules Summary INTRODUCTION Modules are files that contain various functions, variables or classes which are logically related in some manner. Modules like functions are used to implement modularity feature of OOPs concept. Related operations can be grouped together in a file and can be imported in other files. Package is a collection of modules and other sub-modules. Modules can be well organized and easily accessible if collectively stored in a package. MODULE CREATION AND USAGE Module is a logical group of functions , classes, variables in a single python file saved with.pyextension.In other words, we can also say that a python file is a module. We have seen few examples of built-in modules in previous chapters like os, shutil which are used in the program by import statement. Python provides many built-in modules. We can also create our own module. MODULE CREATION AND USAG A major benefit of a module is that functions, variable or objects defined in one module can be easily used by other modules or files, which make the code re-usable. A module can be created like any other python file. Name of the module is the same as the name of a file. Let us create out first module- series.py. In this module, we have created three functions- to find factorial of a number, fibinacci series upto given number of terms and a function to display exponential series and it sum. After creating a file, save it with name series.py. Note: it is important to check where we have saved this file or module. Currently, it is saved in my present working directory. You can check current working directory by using built-in function getcwd() under os module by using following syntax in console window or python prompt. Example 1: Creating module named series.py Importing a module Importing is the process of loading a module in other modules or files. It is necessary to import a module before using its functions, classes or other objects. It allows users to reference its objects. There are various ways of importing a module. 1. using import statement 2. using from import statement 3. using from import * statement 1- Importing Complete module In this method, we can import the whole module all together with a single import statement. In this process, after importing the module, each function (or variable, objects etc.) must be called by the name of the module followed by dot (.) symbol and name of the function. Syntax of function calling within module import module imodule.function_name() This can be done by running the code below in console window directly or in a python file. In this method, other functions or objects present in module random can be called similarly. Here,random () is function present within module random. 2- Importing using from import statement In this method of importing, instead of importing the entire module function or objects, only a particular object needed can be imported. In this method, objects can be directly accessed with its name. 3- Importing entire module using from import * This method can be used to import the entire module using from import * statement. Here,*represents all the functions of a module. Like previous method, an object can be accessed directly with its name.

Use Quizgecko on...
Browser
Browser