Cara menggunakan multiple global variables python

Variables that are created outside of a function (as in all of the examples above) are known as global variables.

Global variables can be used by everyone, both inside of functions and outside.

Example

Create a variable outside of a function, and use it inside the function

x = "awesome"

def myfunc():
  print("Python is " + x)

myfunc()

Try it Yourself »

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

Example

Create a variable inside a function, with the same name as the global variable

x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)

Try it Yourself »


Learn to Filter Data in Python Like a Data Analyst

Cara menggunakan multiple global variables python

Try a hands-on training sessions with step-by-step guidance from an expert. Try the guided project made in collaboration with Coursera now!

Get started


The global Keyword

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use the global keyword.

Example

If you use the global keyword, the variable belongs to the global scope:

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)

Try it Yourself »

Also, use the global keyword if you want to change a global variable inside a function.

Example

To change the value of a global variable inside a function, refer to the variable by using the global keyword:

Setting up a variable in the module will work as a global variable you can define multiple variables in the class. The reason for this is that python treats classes as modules.

Python global multiple variables example

Simple example code defines multiple global variables from inside a class.

This is the best way to set up a list of global variables would be to set up a class for them in that module. You can call them, update them, etc. in any other function by referencing them as such:

main.py

class globalBS():
    bsA = "a"
    bsB = "b"

Test.py

from main import globalBS

print(globalBS.bsA)

globalBS.bsB = "Hello"
print(globalBS.bsB)

Output:

Cara menggunakan multiple global variables python

Note: The global the statement does not create variables. It just makes Python look for them in the global namespace instead of the local namespace.

Source: stackoverflow.com

Do comment if you have any doubts or suggestions on this Python variable tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Cara menggunakan multiple global variables python

Rohit

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

In Python, a variable declared outside the function or in global scope is known as a global variable. We can use global variables both inside and outside the function.

The scope of a global variable is broad. It is accessible in all functions of the same module.

Cara menggunakan multiple global variables python
Cara menggunakan multiple global variables python
Python global variables

Let’s understand it with an example.

Example:

In this example, we declared a global variable

# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
2 with the value ‘Jessa’. The same global variable
# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
2 is accessible to everyone, both inside of functions and outside.

# global variable
name = 'Jessa'

def my_func():
    # access global variable inside function
    print("Name inside function:", name)

my_func()

# access global variable outside function
print('Name Outside function:', name)

Output:

Name inside function: Jessa
Name Outside function: Jessa

Using Global Variables In Function

We can use global variables across multiple functions of the same module.

Now, let’s see how to use the global variable inside a Python function.

  • First, create a global variable
    # global variable
    x = 20
    
    def add():
        # local variable y
        y = 30
        print('local variable y=', y)
    
        # Use global variable x
        print('global variable x=', x)
        z = x + y
        print('x+y=', z)
    
    def sub():
        # local variable m
        m = 10
        print('local variable m=', m)
    
        # Use global variable x in second function
        print('global variable x=', x)
        n = x - m
        print('x-m=', n)
    
    add()
    sub()
    4 and initialize it to 20. The same global variable x is accessible to everyone, both inside of functions and outside.
  • Now, create a function with a combination of local variables and global variables.
  • Create a local variable y And initialize it to 30. A local variable is declared inside the function and is not accessible from outside it. The local variable’s scope is limited to that function only where it is declared.
  • In the end, add a global variable
    # global variable
    x = 20
    
    def add():
        # local variable y
        y = 30
        print('local variable y=', y)
    
        # Use global variable x
        print('global variable x=', x)
        z = x + y
        print('x+y=', z)
    
    def sub():
        # local variable m
        m = 10
        print('local variable m=', m)
    
        # Use global variable x in second function
        print('global variable x=', x)
        n = x - m
        print('x-m=', n)
    
    add()
    sub()
    4 and local variable
    # global variable
    x = 20
    
    def add():
        # local variable y
        y = 30
        print('local variable y=', y)
    
        # Use global variable x
        print('global variable x=', x)
        z = x + y
        print('x+y=', z)
    
    def sub():
        # local variable m
        m = 10
        print('local variable m=', m)
    
        # Use global variable x in second function
        print('global variable x=', x)
        n = x - m
        print('x-m=', n)
    
    add()
    sub()
    6 to calculate the sum of two variables.

Example:

# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()

Output:

local variable y= 30
global variable x= 20
x+y= 50

local variable m= 10
global variable x= 20
x-m= 10

Global Variable and Local Variable with Same Name

Note: If you create a new local variable inside a function with the same name as a global variable, it will not override the value of a global variable. Instead, the new variable will be local and can only be used inside the function. The global variable with the same name will remain unchanged.

Example:

# global variable
x = 20

def my_func():
    # local variable with same name
    # it will not change global variable x
    x = 50
    print('local variable x=', x)

    # modify local variable x
    x = 100
    print('local variable x=', x)

my_func()
print('Global variable x=', x)

Output:

local variable x= 50
local variable x= 100
Global variable x= 20

# global variable x = 20 def add(): # local variable y y = 30 print('local variable y=', y) # Use global variable x print('global variable x=', x) z = x + y print('x+y=', z) def sub(): # local variable m m = 10 print('local variable m=', m) # Use global variable x in second function print('global variable x=', x) n = x - m print('x-m=', n) add() sub()0 Keyword in Python

The

# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
0 keyword is used in the following two cases.

  • To access and modify a global variable inside a function
  • To create a new global variable inside a function

Modify a global variable inside a function

let’s see the below code.

# global variable
x = 20

def my_func():
    # modify global variable x
    x = x + 30

my_func()

Output:

UnboundLocalError: local variable 'x' referenced before assignment

Execute the above code to change the global variable x’s value. You’ll get an

# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
9 because Python treats
# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
4 as a local variable, and
# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
4 is also not defined inside my_func(). i.e, You cannot change or reassign value to a global variable inside a function just like this.

Use the

# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
0 keyword to change the value of a global variable inside a function.

Example:

# global variable
x = 20

def my_func():
    # modify global variable x using global keyword
    global x
    x = x + 30
    print('global variable x inside a function:', x)

# Value of global variable before calling a function
print('global variable x outside a function:', x)

# Value of global variable after calling a function
my_func()
print('global variable x outside a function:', x)

Output:

global variable x outside a function: 20
global variable x inside a function: 50
global variable x outside a function: 50

Create a global variable inside a function

in Python, the scope of variables created inside a function is limited to that function. We cannot access the local variables from outside of the function. Because the scope is local, those variables are not visible outside the function.

To overcome this limitation, we can use the

# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
0 keyword to create a global variable inside a function. This global variable is accessible within and outside the function.

Example:

Name inside function: Jessa
Name Outside function: Jessa
0

Output:

Name inside function: Jessa
Name Outside function: Jessa
1

Rules of # global variable x = 20 def add(): # local variable y y = 30 print('local variable y=', y) # Use global variable x print('global variable x=', x) z = x + y print('x+y=', z) def sub(): # local variable m m = 10 print('local variable m=', m) # Use global variable x in second function print('global variable x=', x) n = x - m print('x-m=', n) add() sub()0 keyword

Let us see the rules we need to follow to create and use a global keyword.

  • If we create a variable inside a function, it is a local variable by default.
  • If we create a variable outside the function, it turns into a global variable, and we don’t have to use the keyword global.
  • We use the keyword global to create a global variable inside a function or to change a global variable already declared outside the function.
  • Using the global keyword outside the function does not make any difference.

Global Variables Across Python Modules/Files

By default, the global variables are accessible across multiple functions of the same module. Now, we’ll see how to share global variables across the modules.

  • First, create a special module
    local variable y= 30
    global variable x= 20
    x+y= 50
    
    local variable m= 10
    global variable x= 20
    x-m= 10
    5 and create global variables in it.
  • Now, import the config module in all application modules, then the module becomes available for a global name.

Let us understand it using an example.

In Python, to create a module, write Python code in the file, and save that file with the

local variable y= 30
global variable x= 20
x+y= 50

local variable m= 10
global variable x= 20
x-m= 10
6 extension.

Example: Share global variables across Python modules.

local variable y= 30
global variable x= 20
x+y= 50

local variable m= 10
global variable x= 20
x-m= 10
5: The config module stores global variables of school and grade

Name inside function: Jessa
Name Outside function: Jessa
2

Now, run the

local variable y= 30
global variable x= 20
x+y= 50

local variable m= 10
global variable x= 20
x-m= 10
5 file.

local variable y= 30
global variable x= 20
x+y= 50

local variable m= 10
global variable x= 20
x-m= 10
9: create a
local variable y= 30
global variable x= 20
x+y= 50

local variable m= 10
global variable x= 20
x-m= 10
9 file to import global variables and modify them. In the
local variable y= 30
global variable x= 20
x+y= 50

local variable m= 10
global variable x= 20
x-m= 10
9 file, we import the config.py module and modify the values of the name and address.

Name inside function: Jessa
Name Outside function: Jessa
3

Now, run the

local variable y= 30
global variable x= 20
x+y= 50

local variable m= 10
global variable x= 20
x-m= 10
9 file.

# global variable
x = 20

def my_func():
    # local variable with same name
    # it will not change global variable x
    x = 50
    print('local variable x=', x)

    # modify local variable x
    x = 100
    print('local variable x=', x)

my_func()
print('Global variable x=', x)
3:

Now, in the

# global variable
x = 20

def my_func():
    # local variable with same name
    # it will not change global variable x
    x = 50
    print('local variable x=', x)

    # modify local variable x
    x = 100
    print('local variable x=', x)

my_func()
print('Global variable x=', x)
4 file, we import both
# global variable
x = 20

def my_func():
    # local variable with same name
    # it will not change global variable x
    x = 50
    print('local variable x=', x)

    # modify local variable x
    x = 100
    print('local variable x=', x)

my_func()
print('Global variable x=', x)
5 and
# global variable
x = 20

def my_func():
    # local variable with same name
    # it will not change global variable x
    x = 50
    print('local variable x=', x)

    # modify local variable x
    x = 100
    print('local variable x=', x)

my_func()
print('Global variable x=', x)
6 modules to test the values of global variables and whether they are changed.

Name inside function: Jessa
Name Outside function: Jessa
4

Output:

Name inside function: Jessa
Name Outside function: Jessa
5

As you can see in the output, we successfully accessed and modified the global variables across the files or modules.

globals() function in Python

In this section, we’ll see what the globals() do in Python.

We can also use the

# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
1 function to access and modify the global variables. The
# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
1 function returns the dictionary of the current global symbol table.

The global symbol table stores all information related to the program’s global scope and is accessed using the

# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
1 method.

Function and variables are not part of any class, or functions are stored in a global symbol table.

Example: Modify the global variable using the

# global variable
x = 20

def add():
    # local variable y
    y = 30
    print('local variable y=', y)

    # Use global variable x
    print('global variable x=', x)
    z = x + y
    print('x+y=', z)

def sub():
    # local variable m
    m = 10
    print('local variable m=', m)

    # Use global variable x in second function
    print('global variable x=', x)
    n = x - m
    print('x-m=', n)

add()
sub()
1 function.

Name inside function: Jessa
Name Outside function: Jessa
6

Output:

Name inside function: Jessa
Name Outside function: Jessa
7

Global variables in Nested Function

Now, Let’s see how to use a global variable in a nested function. Global variables can be used in a nested function using global or nonlocal keywords.

The difference between nonlocal and global is that global is used to change global variables, while nonlocal is used to change variables outside the function. Let us illustrate this with an example.