Python Global variables

The scope of a variable in Python refers to the part of the code where the variable can be accessed and used. In Python, there are two types of scope:

  1. Global scope
  2. Local scope

Global scope (Global variables):

Variables declared outside of any function or class have global scope and can be accessed from anywhere in the code.

Local scope (Local variables):

Variables declared inside a function or class have local scope and can only be accessed within the function or class where they were declared.

Python Global variables

A global variable is a variable that can be accessed from anywhere in the code, i.e. it has a global scope. Global variables are declared outside of any function or class and can be used by any function in the code. To declare a global variable, you use the "global" keyword before the variable name.

How to create a global variable in Python?

x = 10 #Declaring a global variable def my_function(): print("Global Value : " , x) my_function() # Output: Global Value : 10

In the above example, the variable x is declared outside of any function and thus has global scope. It can be accessed from anywhere in the code, including from within the my_function function. When the my_function function is called, it prints the value of the global variable x, which is 10.

Python shadowing

Shadowing in Python refers to the situation where a local variable in a function has the same name as a global variable, and thus "shadows" the global variable within the scope of the local variable. When a local variable and a global variable have the same name in Python, the local variable will take precedence over the global variable within the scope of the local variable.

x = 10 # global variable def my_function(): x = 5 # local variable name same as global print(x) my_function() # Output: 5 print(x) # Output: 10

In the above example, you have a global variable named x with a value of 10. Within the my_function function, you declare a local variable named x with a value of 5. When the my_function function is called, it prints the value of the local variable x, which is 5. When you print the value of the global variable x outside of the function, it is still 10.

Python Global keyword


Python global keyword

The global keyword in Python is used to indicate that a variable is a global variable, rather than a local variable. It allows you to access and modify the value of a global variable from within a function.

x = 10 #lobal variable def my_function(): global x x = 5 print(x) my_function() #Output: 5 print(x) #Output: 5

In this example, you have a global variable x with a value of 10. Within the my_function function, you use the global keyword to indicate that you want to access and modify the value of the global variable x, not create a new local variable with the same name. Then you change the value of x to 5. When you call the my_function function, it prints the new value of x, which is 5. When you print the value of x outside of the function, it is also 5, because the global variable has been modified.

Python Local Variable

A local variable is a variable declared inside a function or class. Local variables have a local scope, which means that they can only be accessed and used within the function or class where they were declared.

def my_function(): x = 5 # local variable print(x) my_function() #Output: 5 print(x) #Raises a NameError: name 'x' is not defined

In the above example, the variable x is declared as a local variable within the my_function function with a value of 5. When the my_function function is called, it prints the value of the local variable x, which is 5. However, if you try to access x outside of the function, a NameError will be raised because x is not defined in the global scope. On the other hand, global variables can be accessed from anywhere in the code. However, it is generally considered best practice to avoid using global variables as much as possible, as they can make the code harder to understand and maintain.

Python variable's lifetime


Python global variable

It's important to keep in mind that local variables are only accessible within their scope, and are destroyed once the function or class is finished executing. This is known as the variable's lifetime. When a function is called again, a new instance of the local variable is created and its value is not remembered from the previous call.

Conclusion

By contrast, global variables are accessible from anywhere in the code, and their lifetime extends from the time they are defined until the end of the program. However, as mentioned earlier, it is generally considered best practice to avoid using global variables as much as possible, as they can make the code harder to understand and maintain.