[Tutor] Another question on global variables...

andy surany mongo57a@comcast.net
Thu, 17 Oct 2002 09:47:08 -0400


ok, I think I'm straight on globals. Thanks everyone.

Alan - regarding your OO example, if I have to assign the result to an
external variable, isn't that the same as a global? Seems like I'm kinda
defeating the purpose of using a function over a global. Or have I
misunderstood?

-Andy
-----Original Message-----
From: alan.gauld@bt.com <alan.gauld@bt.com>
To: mongo57a@comcast.net <mongo57a@comcast.net>; tutor@python.org
<tutor@python.org>
Date: Thursday, October 17, 2002 5:52 AM
Subject: RE: [Tutor] Another question on global variables...


>If I define a global variable at the beginning of my program, like:
>
>    global abc
>    abc=[]
>
>I think you are misunderstanding the 'global' keyword.
>
>If you define any variable at the module level it is global
>(at least within that module!). You only need the global
>keyword to override local scope within a function.
>
>----- Ex code ------
>foo = 1
>
>def bar():
>   foo = 5   # local foo
>
>def baz():
>   global foo
>   foo = 5   # changes the first file level foo.
>--------------------
>
>>Class numberone:
>>    def set_it:
>>        abc=['1','2']
>>        # This works fine and the variable is used throughout the class.
>
>Depends what you mean, it assigns the list to a local variable
>of the set_it method.... If you want to access the global abc you need:
>
>    def set_it():
>        global abc
>        abc = ['1','2']
>
>Then reference it like:
>
>>Class numbertwo:
>>    def ref_it
>>        size=len(abc)
>>        # Here, it appears that abc is [] so the length is zero. Should be
>2
>>
>>Why doesn't this work?
>
>This is referencing the global abc which is indeed empty.
>
>> Are global assignments unique to the class which made
>> the assignment?
>
>No, you never assigned to the global variable, only to a local
>within the method. That variable was lost when the method terminated.
>
>> Or more specifically, do I need to reference a global based
>> on class, such as numberone.abc?? (or???)
>
>That would only work if you had assigned to self.abc within
>the set_it method. (Which is the preferred way of doing thois BTW)
>Your list dissapeared when set_it terminated.
>
>> And being somewhat ignorant of programming, if I drop the
>> whole idea of globals and try to use a function, how would
>> it work?
>
>If you really want to access global variables within a function
>you must explicitly describe the variable as global within each
>function that uses it. A much better way is to make the global
>a parameter of the function and pass it in at call time:
>
>------- Example ------
>
>abc = []   # global var
>
>def nice_set_it(n):
>   L = [n,n]  # use passed parameter wehich might be the global...
>   return L  # pass back a value
>
>def bad_set_it():
>   global abc
>   abc = [abc,abc]
>
># bad_set_it()     # same effect as below, but bad practice
>abc = set_it(abc)  # set abc to a new value based on the old value
>-----------------------
>
>Better still is to put the global variable in a suitable clas
>with the methods that operate on it:
>
>------- OO Example ------
>
>class ABC:
>   self.abc = []
>   def set_it(self): self.abc = [1,2]
>   def get_it(self): return self.abc
>
>foo = ABC()
>foo.get_it()  # prints initial empty list
>foo.set_it    # sets new list
>print foo.get_it()  # prints new value
>---------------------------
>
>> def assign(request_type):
>>    if request_type==obtain:
>>        abc = askstring.........
>>    return abc
>>
>> Once I leave the function, isn't abc a null variable?
>
>Yes but you can assign the result to a variable outside
>the function - or print it...
>
>HTH, Try reading my topic 'Whats in aname' in the
>advanced section of my online tutor for more examples
>etc....
>
>Alan g.
>Author of the 'Learning to Program' web site
>http://www.freenetpages.co.uk/hp/alan.gauld
>
>