
Global Variable and Local variable
In the Variables article, we've explored the concept of variables. Now, let's delve deeper into the different types of variables. Broadly speaking, variables in Python can be categorized into two main types: global variables and local variables.
Local Variable
A local variable is a variable declared within a function or a block of code.
- Local variables are accessible only within that specific function or block.
- Local variables have limited scope and are not visible or accessible outside of the function or block in which they are defined.
Example
# Create a function def my_function(): # Initialize a variable x with value 10 # x is a local variable x = 10 print("Access x inside the function: ", x)
my_function()
Output:
Access x inside the function: 10
The variable x is defined within my_function
, making it accessible inside the function for the print method.
However, tring to access the local variable x outside the function will result in an error:
# This will result in NameError: name 'x' is not definedprint("Access x outside the function: ", x)
Output:
---------------------------------------------------------------------------NameError Traceback (most recent call last)Input In [4], in <cell line: 2>() 1 # This will result in NameError: name 'x' is not defined----> 2 print("Access x outside the function: ", x)
NameError: name 'x' is not define
The NameError indicates that no variable named x is found in the current scope.
Global Variable
A global variable is a variable declared outside of any function or block of code.
- Global variables are accessible from anywhere within the program, including inside functions.
Example
# global variableglobal_var = 20
def my_function(): # Accessing global variable insdie the function print("Access global_var inside a function: ", global_var)
my_function()
print("Access global_var outside a function: ", global_var)
Output:
Access global_var inside a function: 20
Access global_var outside a function: 20
In this example, global_var
is a global variable because it's defined outside of any function, making it accessible to the print
method both inside and outside of my_function
.
One variable can be defined both inside and outside of a function.
When a variable is defined both inside and outside of a function, they operate as entirely separate entities, independent of each other, and do not impact one another's values or behavior.
In the example below, global_var
is initially defined as a global variable and then redefined as a local variable within my_function
. Printing global_var
inside my_function
yields a value of 30, whereas its value is 20 when printed outside the function.
global_var = 20 # Define a global variable
def my_function(): global_var = 30 # modifying the global variable inside the function # Print the value of global_var inside the function print("Value of gloabl_var inside the function: ", global_var)
my_function()
print("Value of gloabl_var outside the function: ", global_var)
Output:
Value of gloabl_var inside the function: 30
Value of gloabl_var outside the function: 20
While syntactically correct, it's not recommended to use the same variable name in two different scopes. Best practice suggests creating unique variable names for clarity.
What if we want to modify the global variable inside a function?
In this case, we need to explicitly declare the variable using the global
keyword.
Modifying a Global Variable Inside a Function
# global variableglobal_var = 20
def my_function(): # declaring the intention to use the global variable global_var global global_var
# modifying the global variable inside the function global_var = 30 print("Value of gloabl_var inside the function: ", global_var)
my_function()
print("Value of gloabl_var outside the function: ", global_var)
Output:
Value of gloabl_var inside the function: 30
Value of gloabl_var outside the function: 30
By declaring global global_var
inside the function, the global_var
no longer acts as a new local variable but refers to the global variable instead.