DEV Community

Avnish
Avnish

Posted on

How to use a global variable in a function?

Using global variables within functions in Python can be handy for sharing common data across various parts of a program. However, it's crucial to use them judiciously to maintain code clarity and avoid unintended side effects. Here are five examples to illustrate different scenarios involving global variables.

Example 1: Accessing a Global Variable

x = "awesome"

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

myfunc()
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • x is defined as a global variable with the value "awesome".
  • myfunc() accesses the global variable x and prints "Python is awesome".

Output:

Python is awesome
Enter fullscreen mode Exit fullscreen mode

Example 2: Local Variable Shadowing Global Variable

x = "awesome"

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

myfunc()
print("Python is " + x)
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The global variable x has the value "awesome".
  • Inside myfunc(), a new local variable x is defined with the value "fantastic". This shadows the global variable x within the function's scope.
  • After calling myfunc(), the global x remains unchanged.

Output:

Python is fantastic
Python is awesome
Enter fullscreen mode Exit fullscreen mode

Example 3: Modifying Global Variable Inside a Function

x = "awesome"

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

myfunc()
print("Python is " + x)
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Initially, x is a global variable with the value "awesome".
  • myfunc() modifies x by declaring it as global within the function and then changing its value to "fantastic".
  • The change is reflected globally.

Output:

Python is fantastic
Enter fullscreen mode Exit fullscreen mode

Example 4: Using Global Variables Across Multiple Functions

counter = 0

def increment():
    global counter
    counter += 1

def print_counter():
    print("Counter:", counter)

increment()
increment()
print_counter()
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • counter is a global variable.
  • increment() increments counter by 1 each time it is called.
  • print_counter() prints the current value of counter.
  • Calling increment() twice and then print_counter() reflects the global counter's value.

Output:

Counter: 2
Enter fullscreen mode Exit fullscreen mode

Example 5: Global Variable and Local Scope

message = "original"

def outer():
    message = "outer"

    def inner():
        global message
        message = "inner"

    print("Before inner():", message)
    inner()
    print("After inner():", message)

outer()
print("In global scope:", message)
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The global variable message is initially set to "original".
  • outer() defines a local variable message with the value "outer".
  • inner() is defined within outer() and modifies the global message to "inner".
  • The prints demonstrate the scope of changes at each step.

Output:

Before inner(): outer
After inner(): outer
In global scope: inner
Enter fullscreen mode Exit fullscreen mode

These examples demonstrate how global variables interact with local scopes and how the global keyword is used to modify global variables within functions. They show the importance of understanding scope to manage variables effectively in Python.

Top comments (0)