Difference Between Local and Global Variable in Python
Variables have a very important role in storing data and information. Before discussing the Difference Between Local and Global Variable in Python, let's see what the scope of variable really is.
Scope of Variable
A variable's scope is basically the lifespan of that variable. It is the section of code to which a variable is alive. Depending on their scope, variables are divided into:
- Local variables
- Global variables
Local variables can only be accessed within the function or module in which they are defined, in contrast to global variables, which can be used throughout the entire program.
In Python, Global variable can be defined using global Keyword, also we can make changes to the variable in local context.
There are some key Differences Between Local and Global Variable in Python:
- Global variables are declared outside the functions whereas local variables are declared within the functions.
- Local variables are created when the function starts its execution and are lost when the function ends. Global variables, on the other hand, are created as execution of the program begins and are lost when the program is ended.
- In contrast to global variables, local variables do not offer data sharing.
- While global variables are kept in a fixed location selected by the compiler, local variables are kept on the stack.
- For local variables, parameter passing is necessary, but not for global variables.
- In Python, Global variables can be defined using global Keyword whereas local variables can be defined directly.
Local Variable
Local variables are declared inside the function blocks. In Python, local variables can be declared at any location in the code block.
Only statements that are written inside a function can access local variables. They are secure in the way that no other function or variable of that program can access them.
Local variables are created during the execution of the function and are destroyed once the block has finished. As soon as the execution leaves the block in which a local variable is declared, it loses its content. It happens because local variables are always stored on the stack.
Example:
Let's see a short example of how local variables can be defined.
Output:
Global Variable
Global variables are the types of variables that are declared outside of every function of the program. The global variable, in contrast to local variables, is accessible by all functions in a program. Global variables are not very reliable because any function in the program can alter their value.
They continue to exist until the entire program has been ended. Global variables hold onto their values throughout the program execution. The compiler-determined fixed region of memory where they are stored is the cause.
A global variable is useful when many functions are using the same set of data. Utilizing a lot of global variables could be challenging because they could undergo unwanted changes in value.
Example:
Let's see a short example of how global variables can be defined. Here, we are simply accessing the global variable using the global keyword.
Output:
Difference Between Local and Global Variable
Comparision Basis | Local Variable | Global Variable |
---|---|---|
Definition | Local variables are declared within the functions. | Global variables are declared outside the functions. |
Lifetime | Local variables are created when the function starts its execution and are lost when the function ends. | Global variables are created as execution of the program begins and are lost when the program is ended. |
Data Sharing | Local Variables doesn't offers Data Sharing. | Global Variables Offers Data Sharing. |
Scope | Accessible inside the function. | Accessible throughout the code. |
Storage | Local variables are kept on the stack. | Global variables are kept in a fixed location selected by the compiler. |
Parameter Passing | For local variables, parameter passing is necessary. | For global variables, parameter passing is not necessary. |
Changes in a Variable Value | Changes in a local variable doesn't affect other functions of the program. | Changes in a global variable is reflected throughout the code. |
Naming Restrictions | Different variables with the same name can be declared in different functions. | Global variables should be uniquely named i.e. many variables with the same name cannot exist. |
Memory | If the local variable is not initialized, it defaults to a garbage value. | The global variable takes zero by default if it is not initialised. |
Examples to Understand Differences Between Local Variable Vs Global Variable
Let's see an example where we can more clearly understand the Difference Between Local and Global Variable in Python.
Example of Local Variable:
In this example, we are making a local variable inside the function and trying to access that local variable outside the function.
Output:
We can see that an error pops up while accessing the variable outside the function, as the scope of that variable is only up to that function.
Example of Global Variable:
In this example, we are making a global variable and are trying to access that global variable inside and outside the function.
Output:
We can see that we can access the global variable both outside the function as well as inside the function.
Advantages and Disadvantages of Local Variables
Advantages of Local Variables
- The main advantage of a local variable is that the data is not accidentally changed. Variable declared inside a function makes use of that variable while avoiding adverse side effects.
- When the block containing the variable is executed, a local variable only uses memory for a brief period of time.
Disadvantages of Local Variables
- The local variable's scope is restricted.
- Local Variable prevents sharing of data.
- Since local variables are created and destroyed with each entry and exit from the block, they cannot save the data in-between the function calls.
Advantages and Disadvantages of Global Variables
Advantages of Global Variables
- When dealing with multiple functions in the program that are manipulating the same data, global variables are very helpful.
- The use of a global variable makes it simpler to make changes that are needed to be made throughout the entire programme.
- For global variables, access is available from anywhere or via any program function at random.
Disadvantages of Global Variables
- The use of numerous global variables may lead to the creation of programming errors.
- The main issue it creates is the accidental changes in data that arise as a result of the program's multiple global variables.
- It might also necessitate performing code refactoring, a time-consuming process that involves reorganizing the entire program's code.
Which One is More Useful?
In Python programming, both local and global variables are crucial when writing programs. However, many global variables could take up a lot of memory. It is getting harder to spot an undesireable change in global variables. As a result, it is wise to avoid declaring pointless global variables and using local variables for passing and manipulating data. It is generally good practice to make use of local variables in Python.
Ready to become a Python-certified professional? Scaler Topics Python course offers comprehensive training and a path to certification. Enroll today!
Conclusion
- A reserved memory address for storing values is called a variable. A variable in a Python programme provides the computer with data to be processed.
- Variables in Python have different scopes, representing their lifespan and access within the code.
- Local variables are created when a function starts and destroyed when it ends, while global variables are created when the program begins and lost when it ends.
- Local variables can only be accessed within the function or module in which they are defined, in contrast to Global variables, which can be used throughout the entire program.
- Local variables are declared inside the function blocks whereas Global variables are the type of variables that are declared outside every function of the program.
- Local variables are stored on the stack, with their memory allocated and deallocated as functions are called and return. Global variables are stored in a fixed memory location determined by the compiler.
- Local variables do not offer data sharing between functions, while global variables are accessible by multiple functions, enabling data sharing.
- It is a good practice to use local variables to avoid unnecessary memory consumption and unintended changes in global variables.
- When developing a programme in any language, both local and global variables are equally crucial. However, a sizable portion of the global variables could require a lot of RAM.