Python Global, Local and Nonlocal variables:
--------------------------------------------
Global Variables:
-----------------
A variable declared outside of the function or in global scope is known as a global variable.
This means that a global variable can be accessed inside or outside of the function.
ex:
x = "global"
def foo():
print("x inside:", x)
foo()
print("x outside:", x)
What if you want to change the value of x inside a function?
ex:
x = "global"
def foo():
x = x * 2
print(x)
foo()
error:
UnboundLocalError: local variable 'x' referenced before assignment
Rules of global Keyword:
When we create a variable inside a function, it is local by default.
When we define a variable outside of a function, it is global by default. You don't have to use global keyword.
We use global keyword to read and write a global variable inside a function.
Use of global keyword outside a function has no effect.
ex:
c = 0 # global variable
def add():
global c
c = c + 2 # increment by 2
print("Inside add():", c)
add()
print("In main:", c)
Local Variables:
----------------
A variable declared inside the function's body or in the local scope is known as a local variable.
ex:
def foo():
y = "local"
foo()
print(y)
output:
NameError: name 'y' is not defined
ex2:
def foo():
y = "local"
print(y)
foo()
Global and local variables:
---------------------------
ex:
x = 5
def foo():
x = 10
print("local x:", x)
foo()
print("global x:", x)
Nonlocal Variables:
-------------------
Nonlocal variables are used in nested functions whose local scope is not defined.
This means that the variable can be neither in the local nor the global scope.
We use nonlocal keywords to create nonlocal variables.
ex:
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)
inner()
print("outer:", x)
outer()
output:
inner: nonlocal
outer: nonlocal
__________________ 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 ...
➡️ 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 27: Global Variable | Local Variable | Nonlocal Variable in Python with a duration of hours minute second in good quality, which was uploaded by the user Testing Tutorialspoint 04 October 2022, share the link with friends and acquaintances, this video has already been watched 30 times on youtube and it was liked by 7 viewers. Enjoy your viewing!