global variables: to be or not to be

John Henry john106henry at hotmail.com
Sat Feb 23 01:17:31 EST 2008


On Feb 22, 9:20 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Fri, 22 Feb 2008 19:11:01 -0800 (PST), icarus <rsa... at 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.



More information about the Python-list mailing list