Home > database >  Difference between a global variable and variable defined in main?
Difference between a global variable and variable defined in main?

Time:08-02

I am confused on the difference between creating a global variable vs defining a variable in main. I have a very specific example that I would like explained. Here is the specific code:

def f():
    username = input("Please enter your username: ")
    print("Thank you for entering your username")

#I want to be able to use the "username" variable outside of the "f" function
#and use it multiple times throughout my code
print(f"Your username is: {username}")

Here is the solution I initially thought was correct:

def f():
    global username
    username = input("Please enter your username: ")
    print("Thank you for entering your username")
f()
print(f"Your username is: {username}")

Here is the solution that I was told was the actual correct/preferred way:

def f():
    username = input("Please enter your username: ")
    print("Thank you for entering your username")
    return username

username = f()
print(f"Your username is: {username}")

The reasoning for the second solution was that it is better to return a variable and creating a global variable using the global keyword is very discouraged/should be avoided, but I'm confused because I thought the second solution also creates a variable that is defined in the global scope since they are defining the variable in main (here is the article I read which confirmed this concept of global vs main variables, if someone can confirm this is correct it would be helpful as well since I have multiple questions regarding this).

I am confused on this Python concept and why the second solution is a better/preferred method of solution. Can someone explain?

CodePudding user response:

While working with a single module they will seem to function identically. Once you introduce an additional module, you will run into issues. Take this example:

# lib1.py
def f():
    global username
    username = input("Please enter your username: ")
    print("Thank you for entering your username")

# lib2.py
def g():
    global username
    username = input("Please enter your username: ")
    print("Thank you for entering your username")

# main.py
from .lib1 import f
from .lib2 import g

f()
g()
print(f"Your username is: {username}")

However, the non-global version keeps username scoped to a single module.

# lib1.py
def f():
    username = input("Please enter your username: ")
    print("Thank you for entering your username")
    return username

# lib2.py
def g():
    username = input("Please enter your username: ")
    print("Thank you for entering your username")
    return username

# main.py
from .lib1 import f
from .lib2 import g

username = f()
username = g()
print(f"Your username is: {username}")

CodePudding user response:

By using a local variable you decrease the dependencies between your components,therefore decreasing the complexity of your code

  • Related