[Tutor] global is bad but ...

Dinesh B Vadhia dineshbvadhia at hotmail.com
Wed Nov 14 06:34:45 CET 2007


Alan/Jim:

It's good to hear some pragmatic advice.  

This particular module has 8 small functions that share common data (structures, primarily in arrays and vectors).  I tried passing array_G as a parameter but that doesn't work because everything in the function remains local and I cannot get back the altered data (unless you know better?). 

The 'global' route works a treat so far.

Dinesh

...............................................................
Date: Tue, 13 Nov 2007 23:11:49 -0000
From: "Alan Gauld" <alan.gauld at btinternet.com>
Subject: Re: [Tutor] global is bad but ...
To: tutor at python.org
Message-ID: <fhdavu$lao$1 at ger.gmane.org>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original

"Dinesh B Vadhia" <dineshbvadhia at hotmail.com> wrote

> Consider a data structure (say, an array) that is operated 
> on by a bunch of functions eg.
>
> def function_A
>     global array_G

> def function_B
>     global array_G

> etc...

> On the other hand, wiser heads say that the use of 'global' 
> is bad and that reworking the code into classes and objects 
> is better.

Rather than answer your question directly can I ask, do 
you know *why* wiser heads say global is bad? What 
problems does using global introduce? What problems 
does it solve?

> What do you think and suggest?

I think it's better to understand issues and make informed 
choices rather than following the rules of others.

I suggest you consider whether global is bad in this case 
and what other solutions might be used instead. Then make 
an informed choice. If, having researched the subject you 
don't understand why global is (sometimes) bad ask for 
more info here.

HTH (a little),

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071113/3b5abab2/attachment-0001.htm 


More information about the Tutor mailing list