Local and Global Variables in Python
top of page

Local and Global Variables in Python

Updated: May 18, 2020



Local Variable

In all-purpose, a variable that is defined in a block is available in that block only. It is not reachable outside the block. Such a variable is called a local variable. Formal argument identifiers also act as local variables.

Example: Function with a Local Variable

def SayHey():

user='Raj'

print ("user = ", user)

return

Here, user is a local variable for the SayHey() function and is not reachable outside of it.

Advantages of using Local Variables

  • The use of local variables provide a guarantee that the values of variables will remain intact while the task is running

  • If a number of tasks change a single variable that is running concurrently, then the result may be random. But declaring it as local variable solves this issue as each task will create its own instance of the local variable.

  • We can provide local variables the same name in dissimilar functions because they are only known by the function they are declared in.

  • Local variables are deleted as soon as any function is over and release the memory space which it occupies.

Disadvantages of using Local Variables

  • The debugging process of a local variable is quite complicated.

  • Common data required to pass again and again as data sharing is not possible between modules.

  • They have a very limited scope.


Global Variable

Any variable lie outside any function block is termed as a global variable. Its value is reachable from inside any function.

Example: Function with a Global Variable

user='Raj'

def SayHey():

print ("user = ", user)

return

Here user can access as global variable because it has been defined out of a function.

Advantages of using Global variables

  • We can access the global variable from all the functions or modules in a program

  • We only require declaring global variable single time outside the modules.

  • It is ideally used for storing "constants" as it helps you keep the consistency.

  • A Global variable is helpful when several functions are accessing the same data.

Disadvantages of using Global Variables

  • Too many variables declared as global, then they stay behind in the memory till program execution is completed. This can cause of Out of Memory issue.

  • Data can be customized by any function. Any statement written in the program can change the value of the global variable. This may give changeable results in multi-tasking environments.

  • If global variables are discontinued due to code refactoring, we will need to change all the modules where they are called.

Differences between Local and Global variables.

Parameter

Scope

Local variables is defined inside a function whereas global variables are define outside the function.

Value

If it is not initialized, a garbage value is stored whereas If it is not initialized zero is stored as default.

Lifetime

Local variable is created when the function starts execution and the functions is terminated then it is lost whereas global variables is created before the program's global execution starts and when the program is terminated then it is lost.

Data sharing

Data sharing is not possible by local variables whereas data sharing is possible by global variable.

Parameters

Parameters passing is necessary for local variables to access the value in other function

whereas parameters passing is not necessary for a global variable as it is visible throughout the program

Accessed by

We can access local variables by statements, inside a function in which they are declared whereas we can access global variables by any statement in the program.

Memory storage

It is stored on the stack unless specified whereas It is stored on a fixed location determined by the compiler.



32 views0 comments

Recent Posts

See All

Tell function in python with example

Tell () function can be used to get the position of file handle and returns current position of file object where as Seek () function is used to change the position of the file handle to a given speci

Seek function in python with example

Seek () function is used to change the position of the file handle to a given specific position in python. File handle such as a cursor which defines from where the data has to be read or written in t

bottom of page