Python3 - Global Variables - Use or Avoid?

Hi all,

So I have worked my way through the Python ‘Physics Class’ project on the Comp. Science track.

In the project you create some functions and then use said functions later in another function. The functions I am posting in relation to are the get_force() & get_work() functions. My question is around the use of global variables. In my get_work() function I called a variable that only existed in the get_force() function:

def get_force():
global n_force # Made n_force global as it is used in get_work() function
train_mass = int(input("What is the train’s mass in kg? ")) # Inputs make more flexible
train_acceleration = int(input("What is the train’s acceleration? "))
n_force = train_mass*train_acceleration
print(“The GE train supplies " + str(n_force) + " Newtons of force.”)
return n_force

def get_work():
get_force() # Calls the get_force() function to eliminate repetition
n_distance = int(input("What is the distance of travel? "))
n_work = n_force * n_distance # Calls the global variable - n_force
print(“The GE train does " + str(n_work) + " Joules of work over " + str(n_distance) + " metres.”)

get_work()

Is it best to avoid using global variables in the above way? If so, can I ask why? Is there some security issue with it or possibly breaks code when you work on more complex tasks?

Hi there, welcome to the forums. :slight_smile:

Yeah, I’d say that’s not a great use of global variables. If there’s a genuine need for some value to be available throughout the program, then a global variable is fine. In your case, it’s unnecessary.

The whole reason that you’ve made n_force a global variable is because you want to use it in your get_work function, right?

You’ve gone to the effort of writing a function, get_force, which calculates the force and returns that value to the caller. We can just as easily do the following to the same effect, and avoid introducing an unnecessary global:

def get_force():
    # note the lack of the global variable...
    train_mass = int(input("What is the train’s mass in kg? "))
    train_acceleration = int(input("What is the train’s acceleration? "))
    n_force = train_mass*train_acceleration
    print("The GE train supplies " + str(n_force) + " Newtons of force.")
    return n_force # we can now get a value for n_force whenever we want by calling get_force

def get_work():
    n_force = get_force() # get a value for n_force - reuse variable name, thanks to scoping
    n_distance = int(input("What is the distance of travel? "))
    n_work = n_force * n_distance # this line unchanged, still works. :)
    print("The GE train does " + str(n_work) + " Joules of work over " + str(n_distance) + " metres.")
    return n_work

get_work()

You’ve correctly understood that the functions represent an opportunity to avoid repeating ourselves when we need to do a specific task, but not the best way to get information from one function to another. :slight_smile:

2 Likes

Much appreciated for your reply! It seems so simple now you’ve shown me aha.

1 Like

No worries. You were most of the way there, I think you were just missing the last little bit of how to get the result of get_force into your get_work function. :slight_smile:

If you get stuck with anything else, let us know. :slight_smile:

Check this threat for more depth in the issue (the title is misleading, but the thread is good!)

1 Like