global variables: to be or not to be

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • icarus

    global variables: to be or not to be

    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.








  • subeen

    #2
    Re: global variables: to be or not to be

    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.



    On Feb 23, 9:11 am, icarus <rsa...@gmail.c omwrote:
    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.

    Comment

    Working...