global variables: to be or not to be

subeen tamim.shahriar at gmail.com
Sat Feb 23 05:43:48 CET 2008


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/


On Feb 23, 9:11 am, icarus <rsa... at 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.




More information about the Python-list mailing list