Global Variable Guide

by Alex
Global Variable Guide

A variable that can be accessed from anywhere in the code is called global. It can be defined outside of a block. In other words, a global variable declared outside a function will be accessible inside it. On the other hand, a variable declared inside a certain block of code will only be visible inside the same block – it is called local. Let’s explain these concepts using an example. An Example of Local and Global Variables


def sum():
a = 10 # local variables
b = 20
c = a + b
print("sum:", c)
sum()

Conclusion: Sum: 30. The variable is declared inside a function and can only be used in that function. This local function cannot be accessed in other functions. Global variables are used to solve this problem. Now take a look at this example with global variables:


a = 20 # defined outside the function
b = 10
def sum():
c = a + b # Using global variables
print("Sum:", c)
def sub():
d = a - b # Using global variables
print("Difference:", d)
sum()
sub()

Conclusion:

Sum: 30
Difference: 10

Two global variables were declared in this code: a and b. They are used inside the functions sum() and sub(). Both return a result when called. If you define a local variable with the same name it will take precedence. See how the msg function implements this.


def msg():
m = "Hi, how are you?"
print(m)
msg()
m = "Fine!"  # global variable
print(m)

Conclusion:

Hi, how's it going?
Fine!

A local variable with the same name as the global variable has been declared here. The value of the local variable is printed first and the value of the global one after that.

The global keyword

Python offers the keyword global, which is used to change the value of a global variable in a function. You need it to change the value. Here are some rules for dealing with global variables.

Rules for using global

  • If a value is defined in the output of a function, it will automatically become a global variable.
  • The global keyword is used to declare a global variable inside a function.
  • There is no need to use global to declare a global variable outside a function.
  • Variables referenced inside a function are implicitly global.

An example without using the global keyword.


c = 10
def mul():
c = c * 10
print(c)
mul()

Conclusion:

line 5, in mul
c = c * 10
UnboundLocalError: local variable 'c' referenced before assignment

This code returned an error because an attempt was made to assign a value to a global variable. The value can only be changed with the global keyword.

c = 10
def mul():
global c
c = c * 10
print("Value in function:", c)
mul()
print("Value outside the function:", c)

Conclusion:

Value in function: 100
Value out of function: 100

Here the variable c has been declared in the mul() function with the keyword global. Its value is multiplied by 10 and becomes 100. As the program runs, you can see that changing the value within the function is reflected in the global value of the variable.

Global Variables in Python Modules

The advantage of using the global keyword is the ability to create global variables and pass them between modules. For example, you could create name.py, which would consist of global variables. If you change them, the changes will affect all the places where these variables occur. 1. Create a name.py file to store the global variables:


a = 10
b = 20
msg = "Hello World"

2. Create a file change.py to change variables:

import name
name.a = 15
name.b = 25
name.msg = "Dood bye"

We change the values of a, b and msg. These variables were declared inside name and the module had to be imported to change them. 3. In the third file we output the values of the changed global variables.

import name
import change
print(name.a)
print(name.b)
print(name.msg)

The value has changed. Output:

15
25
Dood bye

Global in nested functions

You can use the keyword global in nested functions.


def add():
a = 15
def modify():
global a
a = 20
print("Before modification:", a)
print("Making changes")
modify()
print("After modification:", a)
add()
print("Value a:", a)

Conclusion:

Before modification: 15
Making change
After change: 15
Value of a: 20

In this code, the value inside add() takes the value of the local variable x = 15. In modify() it is reassigned and takes on a value of 20 thanks to global. This is what appears in the add() variable.

Related Posts

LEAVE A COMMENT