Lifetime & Scope
There are 3 scopes
Local - refers to the local objects available in the current function
Global - refers to global objects available throughout the code execution
Module - refers to global objects of the current module
declaring a variable as global will give it global scope
def myfunc() :
global _var
_var = 'better'
global keyword
temp = 10 # global variable
def func():
temp = 20 # local variable
print(temp) # output 10
func() # output 20
print(temp) # output 10
This can be changed using the global keyword
temp = 10 # global variable
def func():
global temp = 20 # local variable
print(temp) # output 10
func() # output 20
print(temp) # output 20
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext