Global Variables

Aahz Maruch aahz at panix.com
Mon Feb 19 14:57:51 EST 2001


In article <mailman.982611925.2927.python-list at python.org>,
O'Rourke Clodagh-corour01 <corour01 at motorola.com> wrote:
>
>I am having a lot of problems with my variable scope.
>Where exactly should global variables be declared?

Don't.  Seriously.

>I have written a class that reads the contects of an INFORMIX database
>into variables and arrays for manipulation.  Several methods access
>the same arrays/variables.  I read the values in no problem but when I
>try to access them from another function aside from the one they were
>initiated in they all contain PY_VARO ? I s'ppose they cannot be seen
>by the function in which I am trying to use them so where should they
>be declared?

If you have a class, you should be setting the instance variables using
the "self." notation.  That will allow the variables to be accessed by
other methods of the class.  If you're using functions, make sure to
return the value ("value" being a number, string, tuple, list,
dictionary, or class instance) that you want to use, then pass that value
to any other functions that need to access it.  Use one of the mutable
values (list, dictionary, class) if the later functions need to modify
the value.

BTW, much of this would be clearer if you worked your way through the
entire tutorial: http://www.python.org/doc/current/tut/tut.html
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Why doesn't "Just Say NO" include caffeine, nicotine, alcohol, Prozac,
and Ritalin?  --Aahz



More information about the Python-list mailing list