[Tutor] Basic Question

Alan Gauld alan.gauld at yahoo.co.uk
Thu Sep 9 19:46:00 EDT 2021


On 09/09/2021 23:46, Mats Wichmann wrote:
> On 9/9/21 3:58 PM, Osmaan Mysorewala wrote:
> 
>> A followup question for Mr. Wichmann, I've heard that global variables 
>> are not great, and I see that you wrote some pseuduocode on how to fix 
>> it. Still I don't really understand how to implement that in my code. 
>> Everytime I tried using local variables to store the arrays, it gave the 
>> error "local variable referenced before assignment". If possible, do you 
>> think you could go into a bit more depth on how it would work?

I'll come at this from a slightly different angle from Mats so that
hopefully one of the two views will make sese, and hopefully each
reinforce the other.

Almost every program has some form of high level data structure
that gets changed by different functions throughout the running
of the program. The simplest for of this is a global variable
that is accessed by all functions. However, hard won experience
has taught us that this introduces problems that are very hard
to track down (and sometimes even to detect!). So the advice
is to avoid global variables as much as possible.

Global variables also make it much harder to reuse code a
across programs and to work with multiple versions of the data
in parallel. So all in all they are "A Bad Thing".

So what do we do instead?
One option is to put all the top level program code inside
a function, declare some variables inside that function and
then pass those variables into the sub-functions used by
the program. Here is an example of code with 2 sub functions
(sub1 and sub2)

def sub1():
    print var1

def sub2():
   global var1  # see mats post for what this does!
   var1 = var1 +1

var1 = 0
sub1()       # prints 0
print(var1)   # prints 0
sub2()
print(var1)  # prints 1


Now lets convert that to not use global var1:

def sub1(var):
    print var

def sub2(var):
   return var + 1

def main():
    var1 = 0
    sub1(var1)  # prints 0
    print(var1)   # prints 0
    var1 = sub2(var1)
    print(var1)  # prints 1

notice that the code inside main() is very like the top level
code using global var1, but now var1 is a local variable
inside main(). But there are two big differences:

1) Both sub functions take an input parameter of var
(note not just var1, any var. So we can call those functions
and pass any value in. That makes them more reusable
because they don't depend on the existence of a var1.
All the data they need has been passed in as a parameter.

2) The changes to the variable(in sub2) are passed back
as a returned value. The main() code does not depend on
the inner workings of the sub functions. And the sub
functions do not depend on main(), they are now all
completely independent.

This is only a small step forward from global variables
but it's a big step up in improving our ability to debug
the code and detect errors.

Later, you'll discover even more powerful ways to reduce
the use of global variables and to minimise the sharing
of data between functions. But for now this should be
sufficient.

Note: We could have made an intermediate step by not using
main() and just passing global variables into our modified
sub functions. But the extra work of adding main is minimal
and it removes all global variables from the code.

So, in summary:
- Define your functions to have parameters that represent
any external data they need to do their job
- Pass in any data that the function needs to do its work
as arguments when you call the function.
- And changes to the external data should be passed back
via a return statement
- And variable that is to be changed by a function should
be passed into the function and the old value replaced by
the new (or else create a new variable)

That last point is slightly complicated, but note the
line in main():

var1 = sub2(var1)

ie. It passes in the old value of var1 to sub2() and then
assigns the new value returned by the function to var1,
thus replacing the old with the new.


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list