2. Namespaces
• Name acts as a reference to get any python object.
• Variable is used interchangeably with names that refer to values
• Names can refer to functions, classes, Module also.
• Namespace makes sure that all the names can be used without any
conflict.
• Modules, classes and functions have their own namespace.
• Multiple namespaces may have the same name pointing to a
different objects.
Dr.S.Sangeetha, Department of CA, NIT Trichy
3. Modules and Packages
Module: Smallest unit of code reusability
File containing Python function definitions
Package: Logical collection of modules
Often bundles large products and broad functionality
Library: Collection of packages and modules
Dr.S.Sangeetha, Department of CA, NIT Trichy
4. Importing Modules and Packages
import math
math.sqrt(25)
from math import ceil, floor
print(ceil(9.6))
print(floor(8.9))
# Bind a module element to a new local symbol
from math import ceil as C
print(C(5.8))
Dr.S.Sangeetha, Department of CA, NIT Trichy
6. Namespaces
Local Namespace
• Covers the local names inside a function.
• Created for every function called in a program.
• Active until the function returns.
Get local namespace - locals ()
Global Namespace
• Covers the names from various imported modules used in a project.
• Created for every module included in your program.
• Active until the program ends.
Get global namespace - globals ()
Dr.S.Sangeetha, Department of CA, NIT Trichy
7. Namespaces
Built-in Namespace
• Covers the built-in functions and built-in exception names.
• Created as the interpreter starts
• Active until you exit
dir (__builtins__)
Dr.S.Sangeetha, Department of CA, NIT Trichy
8. Namespaces
• Functions and classes have a local namespace - only they can access
• Each module has a global namespace - Code in the module can
access.
• A module needs variable, function, or class from another module -
import them into its global namespace.
• Importing is a way of getting names into the current namespace.
Dr.S.Sangeetha, Department of CA, NIT Trichy
9. Importing Modules – Which is the best Way?
from module import *
• Imports all the names from the given module directly in your current namespace.
• Use any function directly without adding the name of the module as a prefix.
Leads to Namespace Pollution
Dr.S.Sangeetha, Department of CA, NIT Trichy
10. Importing Modules – Which is the best Way?
from math import ceil, floor
ceil(3.7)
floor(3.7)
• Code is more concise
• Namespace pollution is minimum.
• Can’t use any other names from the module
• Function with the same name in your program will overwrite the
definition of the function imported from the module.
Dr.S.Sangeetha, Department of CA, NIT Trichy
11. Importing Modules – Which is the best Way?
import module
• Safe way of importing a module
• Avoiding namespace pollution
• User can define functions whose names match the name of functions
from the module.
• Usage : prefix the name of the module while using the names
import math
math.sqrt(25)
Dr.S.Sangeetha, Department of CA, NIT Trichy
12. Finding Modules
Load if the module is built-in module
search in the current directory
search through directories in pythonpath
search through default installation location
Error if not found : import error
Dr.S.Sangeetha, Department of CA, NIT Trichy
13. Scope
• Scope - where the name can be used without any prefix.
• Names are bound by scope.
• locals (inner most), function, modules, and built-ins (outer most)
• Local scope has all local names in the current function.
• Module level scope has all the global names from the current module.
• Built-in scope has all the built-in names.
Dr.S.Sangeetha, Department of CA, NIT Trichy
14. Scope Resolution
Scope resolution for a given name
• Inner-most – Local Scope
• Move upwards.
• Built-in scope at the Last.
Error if not found : Name Error
Dr.S.Sangeetha, Department of CA, NIT Trichy
15. Reference Book
Guido van Rossum and Fred L. Drake Jr, “An Introduction to Python “
Revised and updated for Python 3.2, Network Theory Ltd., 2011.
Dr.S.Sangeetha, Department of Computer Applications, NIT
Trichy