global variables: to be or not to be

I

icarus

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.
 
M

Matt Nordhoff

icarus said:
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.)
--
 
S

subeen

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.blogspot.com/
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

John Henry

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top