How To Use Global Variable In Python Example

1. Variable Scope.

Generally, variables defined outside of the function are called global variables, and variables defined inside the function are called local variables. Global variables are available in all scopes, and local variables are only available in it’s definition function. The order that different scope variables are called is local variable first, then global variable. That is to say, local variables are used first. So the question is, what do you do if you want to use a global variable in a function, how change the value of a global variable. This example will tell you how.

2. global Keyword.

To solve the problem of using global variables in functions, Python provides global keyword. This keyword allows you to specify the scope of a variable. So if declare a variable in function with global keyword then this variable is refered to global variable.

3. Example 1.

Functions prefer local variables.

>>> str = 'global varialbe'
>>>
>>> def func_1():
...     str = 'local varialbe'
...     print(str)
...
>>> func_1()
local varialbe
>>>
>>> print(str)
global varialbe

4. Example 2.

Use global variables in the absence of local variables.

>>> str = 'global Variable'
>>>
>>> def func_1():
...     print(str)
...
>>> func_1()
global Variable
>>>
>>> print(str)
global Variable

5. Example 3.

As you can see from example 1, an assignment within a function does not change the value of a global variable, so if you want to change global variable’s value inside a function, you should use the global keyword to declare that the variable inside the function is a global scope variable.

>>> str = 'global variable'
>>>
>>> def func_1():
...     global str
...     str = 'local variable'
...     print(str)
...
>>> func_1()
local variable
>>>
>>> print(str)
local variable

6. How To Declare Multiple global Variables.

global variable_1, variable_2, variable_3

7. Special Type.

Python list, dictionary are modifiable, but cannot be reassigned. If reassigned, you need to use global keyrowd inside functions to declare it is a global scope list or dictionary object.

7.1 Modify global list object do not need to use global keyword.

>>> # declare a global list object
... list = ['global', 'python']
>>>
>>>
>>> def func_1():
...     # modify the golbal list object to append a string. Here you do not need to use global keyword to declare it.
...     list.append('java')
...     print(list)
...
>>> func_1()
['global', 'python', 'java']
>>> print(list)
['global', 'python', 'java']

7.2 Reassign global list object without use global keyword will not success.

>>> # declare a global list object
... list = ['global', 'python']
>>>
>>>
>>> def func_1():
...     # reassign list value without global keyword will not change the global variable value. This list is a local variable.
...     list = ['java']
...     print(list)
...
>>> func_1()
['java']
>>> print(list)
['global', 'python']

7.3 Declare global variable list in function.

>>> # declare a global list object
... list = ['global', 'python']
>>>
>>>
>>> def func_1():
...     # declare global list object
...     global list
...     # reassign global list object value.
...     list = ['java']
...     print(list)
...
>>> func_1()
['java']

>>> print(list)
['java']

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.