Basic Python 17: Python Namespace and Scope

Published: 24 September 2022
on channel: Testing Tutorialspoint
92
4

Python Namespace and Scope:
---------------------------
What is Name in Python?
Name (also called identifier) is simply a name given to objects. Everything in Python is an object. Name is a way to access the underlying object.
ex:
when we do the assignment a = 2, 2 is an object stored in memory and a is the name we associate it with. We can get the address (in RAM) of some object through the built-in function id().
ex:
a = 2
print('id(2) =', id(2))

print('id(a) =', id(a))

a = 2 a = a+1 b = 2
= 2 b - 2
a - 2 a - 3 a - 3

What is a Namespace in Python?

a namespace is a collection of names.
you can imagine a namespace as a mapping of every name you have defined to corresponding objects.
Different namespaces can co-exist at a given time but are completely isolated.
A namespace containing all the built-in names is created when we start the Python interpreter and exists as long as the interpreter runs.
This is the reason that built-in functions like id(), print() etc. are always available to us from any part of the program. Each module creates its own global namespace.


Built-in Namespace
Module: Global Namespace
Functional: Local Namespace

Python Variable Scope:

Scope of the current function which has local names
Scope of the module which has global names
Outermost scope which has built-in names

ex:
def outer_function():
b = 20
def inner_func():
c = 30

a = 10

Here, the variable a is in the global namespace. Variable b is in the local namespace of outer_function() and c is in the nested local namespace of inner_function().

When we are in inner_function(), c is local to us, b is nonlocal and a is global. We can read as well as assign new values to c but can only read b and a from inner_function().

other Ex:

def outer_function():
a = 20

def inner_function():
a = 30
print('a =', a)

inner_function()
print('a =', a)


a = 10
outer_function()
print('a =', a)

other ex:

def outer_function():
global a
a = 20

def inner_function():
global a
a = 30
print('a =', a)

inner_function()
print('a =', a)


a = 10
outer_function()
print('a =', a)
__________________ API Automation _________________
➡️ Rest Assured Using Java →    • Rest Assured  
➡️ Karate Framework using Maven →    • Karate Framework Using Maven Project  
_____________ Programing Language ____________________
➡️ Basic Python →    • Basic Python  
➡️ Core Java →    • CoreJava  
___________ Performances Testing ___________________
➡️ JMeter Beginner →    • JMeter Beginner  
➡️ Locust Beginner →    • Locust  
___________ Git and GitHub _____________________________
➡️ Git and GitHub Beginner →    • Git and GitHub Beginner  

_____________Manual Testing ____________________
➡️ Manual Testing →    • Manual Testing  

______________Automation Testing __________________
➡️ Selenium Cucumber Framework using Java →    • Selenium Cucumber BDD Framework with Java ...  
➡️ Robot Framework with Python →    • Python With Robot Framework  
➡️ Beginner Karate Framework using Intellij →    • Karate Framework Beginner  
➡️ Karate Framework with Gradle using eclipse →    • Karate Framework using Gradle Project  
➡️ Basic Selenium WebDriver using Java →    • Selenium WebDriver  
➡️ TestNG Framework →    • TestNG Framework  
➡️ Robot Framework with RIDE →    • RIDE With Robot Framework  
________________ Beginner Jenkins ____________________
➡️ Beginner Jenkins →    • Beginner Jenkins  


On this page of the site you can watch the video online Basic Python 17: Python Namespace and Scope with a duration of hours minute second in good quality, which was uploaded by the user Testing Tutorialspoint 24 September 2022, share the link with friends and acquaintances, this video has already been watched 92 times on youtube and it was liked by 4 viewers. Enjoy your viewing!