Local and Global Variables:
When we declare a variable inside a function, it becomes a local variable. A local variable scope is limited only to that function where it is created. Its value can be accessible only in that function and not outside of that function.
# ------------------------------------------------------------------------------------------------------------------------ #
When we declare a variable inside a function, it becomes a local variable. A local variable scope is limited only to that function where it is created. Its value can be accessible only in that function and not outside of that function.
On the other hand, when a variable declared outside / before the function definition is called the global variable. The scope of the global variable is available to the entire code, can be accessed inside and outside of the function and other part of the code blocks as well.
Example 1: Local variable
def funvar():
x = 10 # local variable
x = x/2
print('The modified value of local variable is: ', x)
# calling the function
funvar()
# Result:
The modified value of local variable is: 5.0
When we to access the local variable after the function definition, it will throw a name error.
print('The value of local variable is:', x)
# Result:
print('The value of local variable is:', x)
NameError: name 'x' is not defined
Example 2: Local and Global variables
y = 2 # global variable
def funvar():
x = 10 # local variable
x = x/y
print('The value of local variable is: ', x)
print('The value of global variable is: ', y)
# calling the function
funvar()
# Result:
The value of local variable is: 10
The modified value of local variable is: 5.0
The value of global variable is: 2
The global Keyword:
In some cases, when the name of the local and global variable is same then the function by default refers to the local variable and ignores the global variable. The global variable is not accessible inside the function but outside of it is accessible.
In such cases, if we wants to access the global variable inside a function, we can do by using the keyword 'global' before the variable name inside the function definition.
Example: Accessing the Global variable in local
x = 10 # global variable
def funvar():
global x # access the global variable in local
print('The value of global variable is: ', x)
y = 2 # the local variable
print('The value of local variable is: ', y)
x = x/y
print('The modified value of global variable is: ', x)
# calling the function
funvar()
# Result:
The value of global variable is: 10
The value of local variable is: 2
The modified value of global variable is: 5.0
# ------------------------------------------------------------------------------------------------------------------------ #
The globals() Function:
In some cases, when the name of the local and global variable is same the it will be difficult to differentiate them inside a function. If we want work with both the local and global variables with the same names, we can use the globals() function.
The globals() is a built in function which returns a table of current global variables in the form of a dictionary.
Example: Accessing the Global variable having the same name as local variable
x = 2 # global variable
def funvar():
x = 10 # the local variable
print('The value of local variable is: ', x)
z = globals()['x'] # assigning the global to local variable
print('The value of global variable is: ', z)
x = x/z
print('The modified value of local variable is: ', x)
Notes:
In the above program, the names of the global variable(x=2) and local variable(x=10). In this case we are calling the global variable inside the function assigning it to the local variable z using the globals() function.
# calling the function
funvar()
print('The value of global variable is: ', x)
# Result:
The value of local variable is: 10
The value of global variable is: 2
The modified value of local variable is: 5.0
The value of global variable is: 2
#--------------------------------------------------------------Thanks--------------------------------------------------------------#
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.