0

I have a python module that contains code for generating a large array, and it contains multiple functions for doing this. This is how I have it right now:

var1 = 0 var2 = 0 var3 = 0 var4 = 0 var5 = 0 var6 = 0 def main(): foo() moo(var1,var2) noo(var6) def foo(): Math using vars def moo(): More math def noo(): More math 

However, I cannot use vars 1-6 without first defying them inside there respective functions, as it throws a "Referenced before assignment" exception. It seems the best way to do this would be to use global, but that seems heavily frowned upon. Why is global not recommended? Would it be acceptable to use it here? How else should I go about this?

2 Answers 2

1

you need to define those variables as global to be used in the function. Here is an example. Using global variables in a function other than the one that created them

Sign up to request clarification or add additional context in comments.

2 Comments

Is global not frowned upon? Is it acceptable to use in a situation like this?
Yes globals should be avoided unless it is just temp piece of code, it is better to pass as parameters.
0

Better would be to pass the variables in as parameters to those functions:

def main(var1, var2, var3, var4, var5, var6): foo(var1, var2, var3) moo(var2,var3) noo(var5) ... def foo(var1, var2, var3): Math using vars def moo(var2, var3): More math def noo(var5): More math 

And then just call main with all 6 parameters.

Comments