SlideShare a Scribd company logo
1 of 15
Download to read offline
Python Namespaces
Dr.S.Sangeetha
Department of Computer Applications
National Institute of Technology
Tiruchirappalli
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
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
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
Namespaces
Dr.S.Sangeetha, Department of CA, NIT Trichy
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
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
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
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
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
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
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
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
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
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

More Related Content

What's hot

Python decorators
Python decoratorsPython decorators
Python decoratorsAlex Su
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and PackagesDamian T. Gordon
 
Python - gui programming (tkinter)
Python - gui programming (tkinter)Python - gui programming (tkinter)
Python - gui programming (tkinter)Learnbay Datascience
 
python Function
python Function python Function
python Function Ronak Rathi
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaEdureka!
 
Numeric Data types in Python
Numeric Data types in PythonNumeric Data types in Python
Numeric Data types in Pythonjyostna bodapati
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonPriyankaC44
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaEdureka!
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in pythonTMARAGATHAM
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Edureka!
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programmingSrinivas Narasegouda
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONLalitkumar_98
 

What's hot (20)

Python decorators
Python decoratorsPython decorators
Python decorators
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
 
Python - gui programming (tkinter)
Python - gui programming (tkinter)Python - gui programming (tkinter)
Python - gui programming (tkinter)
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
python Function
python Function python Function
python Function
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Numeric Data types in Python
Numeric Data types in PythonNumeric Data types in Python
Numeric Data types in Python
 
Python GUI
Python GUIPython GUI
Python GUI
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 

Similar to Namespaces

Introduction to programming language using c
Introduction to programming language using cIntroduction to programming language using c
Introduction to programming language using cTrinity Dwarka
 
package module in the python environement.pptx
package module in the python environement.pptxpackage module in the python environement.pptx
package module in the python environement.pptxMuhammadAbdullah311866
 
Python Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxPython Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxSingamvineela
 
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don GriffinSenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don GriffinSencha
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoopVladislav Hadzhiyski
 
Object-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingObject-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingAllan Mangune
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.pptYonas D. Ebren
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Introduction to Design Patterns in Javascript
Introduction to Design Patterns in JavascriptIntroduction to Design Patterns in Javascript
Introduction to Design Patterns in JavascriptSanthosh Kumar Srinivasan
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.Tarunsingh198
 
CIS110 Computer Programming Design Chapter (11)
CIS110 Computer Programming Design Chapter  (11)CIS110 Computer Programming Design Chapter  (11)
CIS110 Computer Programming Design Chapter (11)Dr. Ahmed Al Zaidy
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Nicole Ryan
 
JavaBasicsCore1.ppt
JavaBasicsCore1.pptJavaBasicsCore1.ppt
JavaBasicsCore1.pptbuvanabala
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++jehan1987
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsRanel Padon
 

Similar to Namespaces (20)

Introduction to programming language using c
Introduction to programming language using cIntroduction to programming language using c
Introduction to programming language using c
 
package module in the python environement.pptx
package module in the python environement.pptxpackage module in the python environement.pptx
package module in the python environement.pptx
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Python Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxPython Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptx
 
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don GriffinSenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
 
Object-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingObject-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & Programming
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Introduction to Design Patterns in Javascript
Introduction to Design Patterns in JavascriptIntroduction to Design Patterns in Javascript
Introduction to Design Patterns in Javascript
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
CIS110 Computer Programming Design Chapter (11)
CIS110 Computer Programming Design Chapter  (11)CIS110 Computer Programming Design Chapter  (11)
CIS110 Computer Programming Design Chapter (11)
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2
 
python.pptx
python.pptxpython.pptx
python.pptx
 
JavaBasicsCore1.ppt
JavaBasicsCore1.pptJavaBasicsCore1.ppt
JavaBasicsCore1.ppt
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Mini training - Moving to xUnit.net
Mini training - Moving to xUnit.netMini training - Moving to xUnit.net
Mini training - Moving to xUnit.net
 

More from Sangeetha S

Introduction to Computers - Hardware
Introduction to Computers - HardwareIntroduction to Computers - Hardware
Introduction to Computers - HardwareSangeetha S
 
Regression models
Regression  modelsRegression  models
Regression modelsSangeetha S
 
R Probability and Statistics
R Probability and StatisticsR Probability and Statistics
R Probability and StatisticsSangeetha S
 
Introduction to Python Objects and Strings
Introduction to Python Objects and StringsIntroduction to Python Objects and Strings
Introduction to Python Objects and StringsSangeetha S
 
Quiz creation and Launching using Socrative
Quiz creation and Launching using SocrativeQuiz creation and Launching using Socrative
Quiz creation and Launching using SocrativeSangeetha S
 

More from Sangeetha S (9)

Flowchart
FlowchartFlowchart
Flowchart
 
Software
SoftwareSoftware
Software
 
Introduction to Computers - Hardware
Introduction to Computers - HardwareIntroduction to Computers - Hardware
Introduction to Computers - Hardware
 
Regression models
Regression  modelsRegression  models
Regression models
 
R Probability and Statistics
R Probability and StatisticsR Probability and Statistics
R Probability and Statistics
 
R Introduction
R IntroductionR Introduction
R Introduction
 
Set
SetSet
Set
 
Introduction to Python Objects and Strings
Introduction to Python Objects and StringsIntroduction to Python Objects and Strings
Introduction to Python Objects and Strings
 
Quiz creation and Launching using Socrative
Quiz creation and Launching using SocrativeQuiz creation and Launching using Socrative
Quiz creation and Launching using Socrative
 

Recently uploaded

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 

Namespaces

  • 1. Python Namespaces Dr.S.Sangeetha Department of Computer Applications National Institute of Technology Tiruchirappalli
  • 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