Sei sulla pagina 1di 3

SESSION 2020-21

Class XII
Computer Science(083)
(CH-3 USING PYTHON LIBRARIES)
Topic :Import Module & Different ways to import a Module
HANDOUT
Name :___________

Import module: It means how to include a module(code written in the module) in a python program, for
sharing , using , accessing etc.

Different ways to import a module


There are many ways to import a module in your program, the one's which you should know are:
 import
 from
 import*

import command: it is basically used to import the entire module saved as ___.py and it is simplest ,
most common way to use modules in our code.
Its syntax is: import modulename1 [,modulename2, ---------]
Example: >>> import math
To use/ access/invoke a function, you will specify the module name and name of the function-
separated by dot (.). This format is also known as dot notation.
Example: math.sqrt(25)
>>> value= math.sqrt (25) # dot notation
Or
import calculator #as done in previous example in calculator.py
value1 = calculator.add(10,12)

value2=calculator.product(10,2)

from command: it is used to import selected object or function from a module, ie. it is used to get a
specific function in the code instead of the complete module file( If we know beforehand which
function(s), we will be needing, then we may use from). For modules having large no. of functions, it is
recommended to use from instead of import.
Its syntax is: >>> from modulename import functionname [, functionname…..]
Example : >>> from math import sqrt
value = sqrt (25)

import * : import* statement can be used to import all names from a module into the current
calling (namespace) module.
Syntax: from modulename import * ( Import everything from the file)

Example: from area import * #import * gives access to all the functions in area module
print(circle_area(12))
print(square_area(12))
print(rectangle_area(12,13))
Module Aliasing : We can create an alias when you import a module by using as keyword. Thus,
Python provides the salient feature of renaming a module at the time of import.

Syntax: import<module_name> as <alias_name>


import calculator as cal
print(cal.x)
cal.add(10,20)
cal.product(10,20)
Output:
100
The sum is: 30
The product is: 200

Member Aliasing : Like module aliasing, member aliasing is also supported and implemented in
Python modules.
Once we have defined an alias name, we should use alias name only and use of original name
should be avoided as it may create ambiguity and may result in an unexpected output or sometimes
even an error.
Example: from calculator import x as x1, add as sum
print(x1)
sum(100,201)
output : ===== RESTART: C:/Users/Desktop/programs python/memberaliasing.py =====
100
The sum is: 301

Namespace : A namespace is a system to have a unique name for each and every object in Python.(An
object might be a variable or a method).
 Python itself maintains a namespace in the form of a Python dictionary.
Example: A directory-file system structure in computers. Needless to say, that one can have
multiple directories having a file with the same name inside of every directory. But one can get the
directed file, one wishes, just by specifying the absolute path to the file.
 On the similar lines, Python interpreter understands what exact method or variable one is trying to
point to in the code, depending upon the namespace.
 So, the division of the word itself gives little more information. Its name (which means name, an
unique identifier) + space (which talks something related to scope).
 Here, a name might be of any Python method or variable and space depends upon the location from
where is trying to access a variable or a method.
Types of Namespace :
There are three types of Python namespaces:
1. Global namespace 2.Local namespace 3.Built-in namespace
 It is the same with a variable scope in Python.
 When Python interpreter runs solely without user-defined functions, methods, etc. Some functions
like print(), id() are always available, these are built-in namespaces
 When user creates a module , a global namespace gets created.
 Then creation of local functions creates the local namespace.
 The built-in namespace encompasses global namespace and global namespace encompasses
local namespace.

Name Resolution (Resolving Scope of a Name)


The scope of variables in a program, broadly classified as global and local.
The entire Python program revolves around the variable scope and their names. For every name
reference within a program, i.e., when you access a variable from within a program or function,
Python follows name resolution rule, also known as LEGB rule.
That is, for every name reference, Python does the following to resolve it:
(i) It checks within its Local environment (LEGB) or local namespace, if it has a variable with the
same name; if yes, Python uses its value. If not, then it moves to step(ii).
(ii) Python now checks the Enclosing environment (LEGB) (e.g., whether there is a variable with the
same name); if yes, Python uses its value. If the variable is not found in the current environment,
Python repeats this step to higher-level enclosing environment, if any. If not, then it moves to
step(iii).
(iii) Python now checks the Global environment(LEGB) whether there is a variable with the same
name; if yes, Python uses its value. If not, then it moves to step(iv).
(iv) Python checks its Built-in environment (LEGB), which contains all built-in variables and
functions of Python, if there is a variable with the same name; if yes, Python uses its value.
otherwise, Python would generate the error:
Name<variable> not defined.
Thus, in a nutshell, LEGB rule means checking in the order of Local, Enclosing, Global, Built-in
environment (namespaces) to resolve a name reference.

Built-in

Global

Enclosed

Local

Potrebbero piacerti anche