[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

global variables: to be or not to be

icarus

2/23/2008 3:11:00 AM

I've read 'global variables' are bad. The ones that are defined as
'global' inside a function/method.

The argument that pops up every now and then is that they are hard to
keep track of. I don't know Python well enough to argue with that.
Just started learning it a few days ago, so I won't get into
philosophical questions such as "why this? Why not that?". I'll take
it as it is, just like I take 1 + 1 = 2 for granted.

So..."global variables taste bad. Avoid them."

But how do I get around it? How do I update and access a variable
anytime I want? Any easy-to-follow examples? Thanks in advance.








4 Answers

Matt Nordhoff

2/23/2008 4:02:00 AM

0

icarus wrote:
> I've read 'global variables' are bad. The ones that are defined as
> 'global' inside a function/method.
>
> The argument that pops up every now and then is that they are hard to
> keep track of. I don't know Python well enough to argue with that.
> Just started learning it a few days ago, so I won't get into
> philosophical questions such as "why this? Why not that?". I'll take
> it as it is, just like I take 1 + 1 = 2 for granted.
>
> So..."global variables taste bad. Avoid them."
>
> But how do I get around it? How do I update and access a variable
> anytime I want? Any easy-to-follow examples? Thanks in advance.

If doing everything inside one function doesn't work, you should use a
class to store state.

Here's a made-up example. Say you create a Python module to make HTTP
request. It stores the URL of the current one in a global variable. But
then anyone who uses your module can only work with one request at a
time. Instead, you should use an object called "HTTPResponse" or
something with an "url" attribute.

I don't know about other languages, but in Python, classes are very easy
to work with, so it should be no big deal. (Well, you may want to change
if your object evaluates to True, or if it's greater or smaller than 20,
or what happens when someone calls str() on it . . . But you usually
shouldn't need to bother with all that.)
--

subeen

2/23/2008 4:44:00 AM

0

Another way to avoid using global variables is to return more than one
values from the function.

Here is an example that may help you to understand it:

def foo(a, b, c):
a += c
b += c
return a, b

a = 5
b = 10
c = 2
print a, b
a, b = foo(a, b, c)
print a, b


regards,
Subeen.
http://love-python.blo...


On Feb 23, 9:11 am, icarus <rsa...@gmail.com> wrote:
> I've read 'global variables' are bad. The ones that are defined as
> 'global' inside a function/method.
>
> The argument that pops up every now and then is that they are hard to
> keep track of. I don't know Python well enough to argue with that.
> Just started learning it a few days ago, so I won't get into
> philosophical questions such as "why this? Why not that?". I'll take
> it as it is, just like I take 1 + 1 = 2 for granted.
>
> So..."global variables taste bad. Avoid them."
>
> But how do I get around it? How do I update and access a variable
> anytime I want? Any easy-to-follow examples? Thanks in advance.

Dennis Lee Bieber

2/23/2008 5:20:00 AM

0

On Fri, 22 Feb 2008 19:11:01 -0800 (PST), icarus <rsarpi@gmail.com>
declaimed the following in comp.lang.python:

>
> But how do I get around it? How do I update and access a variable
> anytime I want? Any easy-to-follow examples? Thanks in advance.
>
Well, if you want to follow the Java decrees, you create a class to
hold it, create an instance of the class (okay, it may be possible to
work without an instance -- other than knowing Java has an overgrown and
incomprehensible standard library, and all the flawed syntax of C and
Pascal, I avoid the language), and in that class you have defined
methods to get and set the value.

Oh... and you pass the instance, as a parameter, in/out of all other
invoked methods that might need it. Thereby there is never a global
reference to it.

--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

John Henry

2/23/2008 6:18:00 AM

0

On Feb 22, 9:20 pm, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> On Fri, 22 Feb 2008 19:11:01 -0800 (PST), icarus <rsa...@gmail.com>
> declaimed the following in comp.lang.python:
>
>
>
> > But how do I get around it? How do I update and access a variable
> > anytime I want? Any easy-to-follow examples? Thanks in advance.
>

<snip>

>
> Oh... and you pass the instance, as a parameter, in/out of all other
> invoked methods that might need it. Thereby there is never a global
> reference to it.
>
> --

For instance:

###############
class global:
def __init__(self, x):
self.x = x
return

myGlobal = global(1.0)

def subA(top):
print top.x

subA(myGlobal)
###############

For me, the reason to avoid global variables is that it's much easier
down the road when I want to make my program ran in an multi-process,
or multi-threaded situation.