Python Global variables

Variables that are created outside of a function are known as Global Variables . A global variable is one that can be accessed anywhere . This means, global variable can be accessed inside or outside of the function.

Python global variable

How to create a Global Variable


In most cases where you are tempted to use a global variable, it is better to utilize a parameter for getting a value into a function or return a value to get it out. Like in many other program structures, Python also imposes good programming habit by design.

How to share a global variable across python modules

A global variable is a variable which is accessible in multiple scopes. In Python, it is better to use a single module to hold all the global variables you want to use and whenever you want to use them, just import this module, and then you can modify that and it will be visible in other modules that do the same.

Create a Global module

Create a Python program file to access global variable

Create another Python program to test value is changed or not

Using global variables in a function

A global can be accessed by any function , but it can only be modified if you explicitly declare it with the 'global' keyword inside the function. To use global variables inside a function, you need to do global < varName > inside the function. Keep in mind, that you only need to declare them global inside the function if you want to do assignments / change them. global is not needed for printing and accessing.



Global and Local Variables in Python

Python global keyword

Local variables

Local variables can only be reached in their scope. For ex. check with the following two local variables.



The error showing because the variable x has only local scope.

NameError: name 'xx' is not defined

Python knows the purposes of certain names (ex. built-in functions ). Other names are defined within the program (ex. variables). If Python encounters a name that it doesn't recognize, you'll probably get NameError: global name 'xx' is not defined error. In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn't know what it's for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as "True". Before you use the global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it.

Moreover, If a variable with same name is defined inside the scope of function as well then it will print the value given inside the function only and not the global value.



An UnboundLocalError is raised when a local variable is referenced before it has been assigned. In most cases this will occur when trying to modify a local variable before it is actually assigned within the local scope. Python doesn't have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local.

Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope , it cannot modify them (unless they're declared global with the global keyword ). A closure binds values in the enclosing environment to names in the local environment. The local environment can then use the bound value , and even reassign that name to something else, but it can't modify the binding in the enclosing environment. UnboundLocalError happened because when python sees an assignment inside a function then it considers that variable as local variable and will not fetch its value from enclosing or global scope when we execute the function. However, to modify a global variable inside a function, you must use the global keyword.

Python Data Types and Variables

Python Global Keyword

Every variable in Python is considered as an object . Variables in Python follow the standard nomenclature of an alphanumeric name beginning in a letter or underscore. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory . You do not need to declare variables before using them, or declare their type. Variable names are case sensitive . Most variables in Python are local in scope to their own function or class.

More on.... Python Data Types and Variables

Looking for a Python job ?

Chances are you will need to prove that you know how to work with Python. These Python Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Python Programming . Here are the top objective type sample Python Interview questions and their answers are given just below to them. These sample questions are framed by our experts team who trains for Python training to give you an idea of type of questions which may be asked in interview.

Go to... Python Interview Questions
 (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About