SlideShare a Scribd company logo
NADAR SARASWATHI COLLEGE
OF ARTS AND SCIENCE
Department of CS &IT
SCOPE OF OBJECTS AND NAMES
Presented by:
S.SABTHAMI
I.MSC(IT)
Python Scopes and Namespaces
• A namespace is a mapping from names to objects.
• Most namespaces are currently implemented as
Python dictionaries, but that’s normally not
noticeable in any way.
• Examples of namespaces are:
– the set of built-in names (functions such as abs(), and
built-in exception names)
– the global names in a module;
– and the local names in a function invocation.
Python Scopes and Namespaces
– In a sense the set of attributes of an object also form a
namespace.
• The important thing to know about namespaces is
that there is absolutely no relation between names
in different namespaces;
– for instance, two different modules may both define a
function “maximize” without confusion — users of the
modules must prefix it with the module name.
Python Scopes and Namespaces
• In the expression modname.funcname,
modname is a module object and funcname is
an attribute of it.
• In this case there happens to be a
straightforward mapping between the
module’s attributes and the global names
defined in the module:
– they share the same namespace!
Python Scopes and Namespaces
• Namespaces are created at different moments and
have different lifetimes.
• The namespace containing the built-in names is
created when the Python interpreter starts up, and is
never deleted.
• The global namespace for a module is created when
the module definition is read in;
– normally, module namespaces also last until the
interpreter quits.
Python Scopes and Namespaces
• The statements executed by the top-level
invocation of the interpreter, either read from
a script file or interactively, are considered
part of a module called __main__,
– so they have their own global namespace.
• The built-in names actually also live in a
module;
– this is called __builtin__.
Python Scopes and Namespaces
• The local namespace for a function is created
– when the function is called
• And deleted
– when the function returns or raises an exception
that is not handled within the function.
– Of course, recursive invocations each have their
own local namespace.
Python Scopes and Namespaces
• A scope is a textual region of a Python
program where a namespace is directly
accessible.
• “Directly accessible” here means that an
unqualified reference to a name attempts to
find the name in the namespace.
Python Scopes and Namespaces
• Although scopes are determined statically,
they are used dynamically.
• At any time during execution, there are at
least three nested scopes whose namespaces
are directly accessible:
– the innermost scope, which is searched first,
contains the local names; the namespaces of any
enclosing functions,
Python Scopes and Namespaces
– which are searched starting with the nearest
enclosing scope; the middle scope, searched next,
contains the current module’s global names;
– and the outermost scope (searched last) is the
namespace containing built-in names.
Python Scopes and Namespaces
• If a name is declared global, then all
references and assignments go directly to the
middle scope containing the module’s global
names.
• Otherwise, all variables found outside of the
innermost scope are read-only.
Python Scopes and Namespaces
• Usually, the local scope references the local
names of the current function.
• Outside of functions, the local scope
references the same namespace as the global
scope:
– the module’s namespace.
• Class definitions place yet another namespace
in the local scope.
Python Scopes and Namespaces
• A special quirk of Python is that assignments
always go into the innermost scope.
• Assignments do not copy data—
– they just bind names to objects.
• The same is true for deletions:
– the statement ‘del x’ removes the binding of x
from the namespace referenced by the local
scope.
Python Scopes and Namespaces
• In fact, all operations that introduce new
names use the local scope:
– in particular, import statements and function
definitions bind the module or function name in
the local scope. (The global statement can be used
to indicate that particular variables live in the
global scope.)
python.pptx

More Related Content

What's hot

Python: Polymorphism
Python: PolymorphismPython: Polymorphism
Python: Polymorphism
Damian T. Gordon
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
Python libraries
Python librariesPython libraries
Python libraries
Prof. Dr. K. Adisesha
 
Python pandas Library
Python pandas LibraryPython pandas Library
Python pandas Library
Md. Sohag Miah
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
Home
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
Edureka!
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
Kevlin Henney
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
Lifna C.S
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Shakti Singh Rathore
 
Introduction To Python
Introduction To PythonIntroduction To Python
Introduction To Python
Vanessa Rene
 
Python-Polymorphism.pptx
Python-Polymorphism.pptxPython-Polymorphism.pptx
Python-Polymorphism.pptx
Karudaiyar Ganapathy
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
Srinivas Narasegouda
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
Sarfaraz Ghanta
 
Python ppt
Python pptPython ppt
Python ppt
Anush verma
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
Haim Michael
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
Girish Khanzode
 
Introduction to python
 Introduction to python Introduction to python
Introduction to python
Learnbay Datascience
 

What's hot (20)

Python: Polymorphism
Python: PolymorphismPython: Polymorphism
Python: Polymorphism
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Python libraries
Python librariesPython libraries
Python libraries
 
Python pandas Library
Python pandas LibraryPython pandas Library
Python pandas Library
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism 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
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Introduction To Python
Introduction To PythonIntroduction To Python
Introduction To Python
 
Python-Polymorphism.pptx
Python-Polymorphism.pptxPython-Polymorphism.pptx
Python-Polymorphism.pptx
 
Function in Python
Function in PythonFunction in Python
Function in Python
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 
Python ppt
Python pptPython ppt
Python ppt
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
Introduction to python
 Introduction to python Introduction to python
Introduction to python
 

Similar to python.pptx

Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Python Namespace.pdf
Python Namespace.pdfPython Namespace.pdf
Python Namespace.pdf
SudhanshiBakre1
 
Namespaces
NamespacesNamespaces
Namespaces
Sangeetha S
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .
tarunsharmaug23
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
Patrick Sheridan
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
vivek p s
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
Kazuhiro Sera
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
Hemlathadhevi Annadhurai
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
introduction of Object oriented programming
introduction of Object oriented programmingintroduction of Object oriented programming
introduction of Object oriented programming
RiturajJain8
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
Prashant Rane
 
dylibencapsulation
dylibencapsulationdylibencapsulation
dylibencapsulationCole Herzog
 
Java
JavaJava
Python with data Sciences
Python with data SciencesPython with data Sciences
Python with data Sciences
Krishna Mohan Mishra
 
Python ppt
Python pptPython ppt
Python ppt
Mohita Pandey
 
Subprogram
SubprogramSubprogram
Subprogram
baran19901990
 

Similar to python.pptx (20)

Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 
Python Namespace.pdf
Python Namespace.pdfPython Namespace.pdf
Python Namespace.pdf
 
Namespaces
NamespacesNamespaces
Namespaces
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
Java
JavaJava
Java
 
About Python
About PythonAbout Python
About Python
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
introduction of Object oriented programming
introduction of Object oriented programmingintroduction of Object oriented programming
introduction of Object oriented programming
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
 
dylibencapsulation
dylibencapsulationdylibencapsulation
dylibencapsulation
 
Java
JavaJava
Java
 
Python with data Sciences
Python with data SciencesPython with data Sciences
Python with data Sciences
 
Python ppt
Python pptPython ppt
Python ppt
 
Subprogram
SubprogramSubprogram
Subprogram
 

More from SabthamiS1

women%20empowerment11.pptx
women%20empowerment11.pptxwomen%20empowerment11.pptx
women%20empowerment11.pptx
SabthamiS1
 
big data analytics.pptx
big data analytics.pptxbig data analytics.pptx
big data analytics.pptx
SabthamiS1
 
iot.pptx
iot.pptxiot.pptx
iot.pptx
SabthamiS1
 
dip.pptx
dip.pptxdip.pptx
dip.pptx
SabthamiS1
 
csc.pptx
csc.pptxcsc.pptx
csc.pptx
SabthamiS1
 
Data minig.pptx
Data minig.pptxData minig.pptx
Data minig.pptx
SabthamiS1
 
artificial intelligence.pptx
artificial intelligence.pptxartificial intelligence.pptx
artificial intelligence.pptx
SabthamiS1
 
distributed computing.pptx
distributed computing.pptxdistributed computing.pptx
distributed computing.pptx
SabthamiS1
 
Network and internet security
Network and internet security Network and internet security
Network and internet security
SabthamiS1
 
Java
Java Java
Java
SabthamiS1
 
Advance computer architecture
Advance computer architecture Advance computer architecture
Advance computer architecture
SabthamiS1
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
SabthamiS1
 

More from SabthamiS1 (12)

women%20empowerment11.pptx
women%20empowerment11.pptxwomen%20empowerment11.pptx
women%20empowerment11.pptx
 
big data analytics.pptx
big data analytics.pptxbig data analytics.pptx
big data analytics.pptx
 
iot.pptx
iot.pptxiot.pptx
iot.pptx
 
dip.pptx
dip.pptxdip.pptx
dip.pptx
 
csc.pptx
csc.pptxcsc.pptx
csc.pptx
 
Data minig.pptx
Data minig.pptxData minig.pptx
Data minig.pptx
 
artificial intelligence.pptx
artificial intelligence.pptxartificial intelligence.pptx
artificial intelligence.pptx
 
distributed computing.pptx
distributed computing.pptxdistributed computing.pptx
distributed computing.pptx
 
Network and internet security
Network and internet security Network and internet security
Network and internet security
 
Java
Java Java
Java
 
Advance computer architecture
Advance computer architecture Advance computer architecture
Advance computer architecture
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 

Recently uploaded

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 

python.pptx

  • 1. NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE Department of CS &IT SCOPE OF OBJECTS AND NAMES Presented by: S.SABTHAMI I.MSC(IT)
  • 2. Python Scopes and Namespaces • A namespace is a mapping from names to objects. • Most namespaces are currently implemented as Python dictionaries, but that’s normally not noticeable in any way. • Examples of namespaces are: – the set of built-in names (functions such as abs(), and built-in exception names) – the global names in a module; – and the local names in a function invocation.
  • 3. Python Scopes and Namespaces – In a sense the set of attributes of an object also form a namespace. • The important thing to know about namespaces is that there is absolutely no relation between names in different namespaces; – for instance, two different modules may both define a function “maximize” without confusion — users of the modules must prefix it with the module name.
  • 4. Python Scopes and Namespaces • In the expression modname.funcname, modname is a module object and funcname is an attribute of it. • In this case there happens to be a straightforward mapping between the module’s attributes and the global names defined in the module: – they share the same namespace!
  • 5. Python Scopes and Namespaces • Namespaces are created at different moments and have different lifetimes. • The namespace containing the built-in names is created when the Python interpreter starts up, and is never deleted. • The global namespace for a module is created when the module definition is read in; – normally, module namespaces also last until the interpreter quits.
  • 6. Python Scopes and Namespaces • The statements executed by the top-level invocation of the interpreter, either read from a script file or interactively, are considered part of a module called __main__, – so they have their own global namespace. • The built-in names actually also live in a module; – this is called __builtin__.
  • 7. Python Scopes and Namespaces • The local namespace for a function is created – when the function is called • And deleted – when the function returns or raises an exception that is not handled within the function. – Of course, recursive invocations each have their own local namespace.
  • 8. Python Scopes and Namespaces • A scope is a textual region of a Python program where a namespace is directly accessible. • “Directly accessible” here means that an unqualified reference to a name attempts to find the name in the namespace.
  • 9. Python Scopes and Namespaces • Although scopes are determined statically, they are used dynamically. • At any time during execution, there are at least three nested scopes whose namespaces are directly accessible: – the innermost scope, which is searched first, contains the local names; the namespaces of any enclosing functions,
  • 10. Python Scopes and Namespaces – which are searched starting with the nearest enclosing scope; the middle scope, searched next, contains the current module’s global names; – and the outermost scope (searched last) is the namespace containing built-in names.
  • 11. Python Scopes and Namespaces • If a name is declared global, then all references and assignments go directly to the middle scope containing the module’s global names. • Otherwise, all variables found outside of the innermost scope are read-only.
  • 12. Python Scopes and Namespaces • Usually, the local scope references the local names of the current function. • Outside of functions, the local scope references the same namespace as the global scope: – the module’s namespace. • Class definitions place yet another namespace in the local scope.
  • 13. Python Scopes and Namespaces • A special quirk of Python is that assignments always go into the innermost scope. • Assignments do not copy data— – they just bind names to objects. • The same is true for deletions: – the statement ‘del x’ removes the binding of x from the namespace referenced by the local scope.
  • 14. Python Scopes and Namespaces • In fact, all operations that introduce new names use the local scope: – in particular, import statements and function definitions bind the module or function name in the local scope. (The global statement can be used to indicate that particular variables live in the global scope.)