4 Simple ways to use Python global function?

Last Updated on June 2, 2021 by admin

In Python, the global keyword permits you to change the variable outside of the current extension so, In this article we will tell you about global function 4 Simple ways to use Python global function.

It is utilized to make a global variable and make changes to the variable in a nearby setting.

Rules of Python Global function:

  • When we create a variable inside a function, it is local by default.
  • When we define a variable outside of a function, it is global by default.
  • You don’t have to use the global keyword.
  • We use a global keyword to read and write a global variable inside a function.
  • Use of global keywords outside a function has no effect.

Use of Python Global Function:

Accessing global Variable From Inside a Function


C = 10 # global variable

Def add():

Print©

Add()

When we run the above program, the output will be:

10

However, we may have some scenarios where we need to modify the global variable from inside a function.

If we need to assign a new value to a global variable then we can do that by declaring the variable as global.

Using Python Global in Nested Functions:

In order to use global inside a nested functions, we have to declare a variable with global keyword inside a nested function


# Python program showing a use of

# global in nested function

Def add():

X = 15

Def change(): Global x

X = 20

Print(“Before making changing: “, x)

Print(“Making change”)

Change()

Print(“After making change: “, x)

Add()

Print(“value of x”,x)

Output:

Before making changing:  15

Making change

After making change:  15

Value of x 20

In the above example Before and after making change(), the variable x takes the value of the local variable i.e x = 15.

Outside of the add() function, the variable x will take value defined in the change() function i.e x = 20.

Because we have used global keyword in x to create a global variable inside the change() function (local scope).

Modifying Global Variable From Inside the Function

The most ideal approach to share global function factors across various modules inside a similar program is to make an exceptional module (frequently named config or cfg).

Import the config module in all modules of your application; the module at that point opens up as a global name.

There is just one example of every module thus any progressions made to the module object get reflected all over


C = 1 # global variable

Def add():

C = c + 2 # increment c by 2

Print©

Add()

When we run the above program, the output shows an error:

UnboundLocalError: local variable ‘c’ referenced before assignment

This is because we can only access the global variable but cannot modify it from inside the function.

The solution for this is to use the python global function.

Share a Global Variable Across Python Modules:

Global variable in Python implies having a scope all through the program, i.e., a global variable worth is open all through the program except if shadowed.

In Python, we create a single module config.py to hold global variables and share information across Python modules within the same program.

A global variable in Python is frequently announced as the highest point of the program.

All in all, factors that are announced outside of a function are known as global factors.

The best way to share global variables across modules across a single program is to create a config module.

Just import the config module in all modules of your application; the module then becomes available as a global name.


Config.py

X = 0

Mod.py:

Import config

Config.x = 1

Main.py:

Import config

Import mod

Print(config.x
<h3>Changing Global Variable From Inside a Function Using Global:</h3>
C = 0 # global variable

Def add():

Global c

C = c + 2 # increment by 2

Print(“Inside add():”, c)

Add()

Print(“In main:”, c)

When we run the above program, the output will be:

Inside add(): 2

In main: 2

In the above program, we define c as a global keyword inside the add() function.

Then, we increment the variable c by 1, i.e c = c + 2.

After that, we call the add() function.

Finally, we print the global variable c.

As we can see, change also occurred on the global variable outside the function, c = 2.

Read More: How to Use Python Lambda functions?

Leave a Comment