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 (20)

PPTX
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
 
PPTX
Functions in Python
Shakti Singh Rathore
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PDF
Introduction to oops concepts
Nilesh Dalvi
 
PPTX
Operator overloading and type conversions
Amogh Kalyanshetti
 
PPTX
Java constructors
QUONTRASOLUTIONS
 
PDF
Introduction To Python | Edureka
Edureka!
 
PPTX
Python programming | Fundamentals of Python programming
KrishnaMildain
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Constructor in java
Madishetty Prathibha
 
PPTX
Python basics
Hoang Nguyen
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
C++ programming function
Vishalini Mugunen
 
PPT
programming with python ppt
Priyanka Pradhan
 
PPSX
Stack
Seema Sharma
 
PPTX
Java package
CS_GDRCST
 
PPTX
Functions in python slide share
Devashish Kumar
 
PPTX
Pure virtual function and abstract class
Amit Trivedi
 
PPT
Function overloading(c++)
Ritika Sharma
 
PPTX
Polymorphism
Ahmed Za'anin
 
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
 
Functions in Python
Shakti Singh Rathore
 
oops concept in java | object oriented programming in java
CPD INDIA
 
Introduction to oops concepts
Nilesh Dalvi
 
Operator overloading and type conversions
Amogh Kalyanshetti
 
Java constructors
QUONTRASOLUTIONS
 
Introduction To Python | Edureka
Edureka!
 
Python programming | Fundamentals of Python programming
KrishnaMildain
 
Python Modules
Nitin Reddy Katkam
 
Constructor in java
Madishetty Prathibha
 
Python basics
Hoang Nguyen
 
Python: Modules and Packages
Damian T. Gordon
 
C++ programming function
Vishalini Mugunen
 
programming with python ppt
Priyanka Pradhan
 
Java package
CS_GDRCST
 
Functions in python slide share
Devashish Kumar
 
Pure virtual function and abstract class
Amit Trivedi
 
Function overloading(c++)
Ritika Sharma
 
Polymorphism
Ahmed Za'anin
 

Similar to python.pptx (20)

PPT
Programming with _Python__Lecture__3.ppt
geethar79
 
PPT
Basics of Programming_Python__Lecture__3.ppt
geethar79
 
PPTX
Object Oriented Programming.pptx
SAICHARANREDDYN
 
PDF
Python Namespace.pdf
SudhanshiBakre1
 
PDF
Namespaces
Sangeetha S
 
PPT
Lesson on Python Classes by Matt Wufus 2003
davidlin271898
 
PPTX
Functions in Python Syntax and working .
tarunsharmaug23
 
PPTX
Python programming Concepts (Functions, classes and Oops concept
Lipika Sharma
 
PDF
Javascript classes and scoping
Patrick Sheridan
 
PPTX
asic computer is an electronic device that can receive, store, process, and o...
vaishalisharma125399
 
PPTX
full defination of final opp.pptx
rayanbabur
 
PPTX
Object oriented java script
vivek p s
 
PPTX
Java
Raghu nath
 
PPTX
About Python
Shao-Chuan Wang
 
PPTX
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
PPTX
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
mru761077
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PPTX
Chapter-3 الشابتر الثالث هياكل بيانات جامعة خالد .pptx
HakimAlHuribi
 
KEY
Learning from "Effective Scala"
Kazuhiro Sera
 
PPT
Introducing object oriented programming (oop)
Hemlathadhevi Annadhurai
 
Programming with _Python__Lecture__3.ppt
geethar79
 
Basics of Programming_Python__Lecture__3.ppt
geethar79
 
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Python Namespace.pdf
SudhanshiBakre1
 
Namespaces
Sangeetha S
 
Lesson on Python Classes by Matt Wufus 2003
davidlin271898
 
Functions in Python Syntax and working .
tarunsharmaug23
 
Python programming Concepts (Functions, classes and Oops concept
Lipika Sharma
 
Javascript classes and scoping
Patrick Sheridan
 
asic computer is an electronic device that can receive, store, process, and o...
vaishalisharma125399
 
full defination of final opp.pptx
rayanbabur
 
Object oriented java script
vivek p s
 
About Python
Shao-Chuan Wang
 
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
mru761077
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Chapter-3 الشابتر الثالث هياكل بيانات جامعة خالد .pptx
HakimAlHuribi
 
Learning from "Effective Scala"
Kazuhiro Sera
 
Introducing object oriented programming (oop)
Hemlathadhevi Annadhurai
 
Ad

More from SabthamiS1 (12)

PPTX
women%20empowerment11.pptx
SabthamiS1
 
PPTX
big data analytics.pptx
SabthamiS1
 
PPTX
iot.pptx
SabthamiS1
 
PPTX
dip.pptx
SabthamiS1
 
PPTX
csc.pptx
SabthamiS1
 
PPTX
Data minig.pptx
SabthamiS1
 
PPTX
artificial intelligence.pptx
SabthamiS1
 
PPTX
distributed computing.pptx
SabthamiS1
 
PPTX
Network and internet security
SabthamiS1
 
PPTX
Java
SabthamiS1
 
PPTX
Advance computer architecture
SabthamiS1
 
PPTX
Data structure and algorithm
SabthamiS1
 
women%20empowerment11.pptx
SabthamiS1
 
big data analytics.pptx
SabthamiS1
 
iot.pptx
SabthamiS1
 
dip.pptx
SabthamiS1
 
csc.pptx
SabthamiS1
 
Data minig.pptx
SabthamiS1
 
artificial intelligence.pptx
SabthamiS1
 
distributed computing.pptx
SabthamiS1
 
Network and internet security
SabthamiS1
 
Java
SabthamiS1
 
Advance computer architecture
SabthamiS1
 
Data structure and algorithm
SabthamiS1
 
Ad

Recently uploaded (20)

PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
community health nursing question paper 2.pdf
Prince kumar
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPT on the Development of Education in the Victorian England
Beena E S
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 

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.)