Modifying Global Variables from Within a Function

Aahz Maruch aahz at netcom.com
Sun Jan 9 16:09:48 EST 2000


In article <8EB69CD0Ejosuetompkins at 209.208.0.7>,
Josh Tomkins <josht at iname.com> wrote:
>
>Can you modify a global variable from inside a function?  

Yup, and Emile gave you the answer for how to do that.  However, I am
going to challenge you to avoid that way of programming, particularly
given that you need to modify several variables.  Here are three good
ways to deal with the situation without using the "global" keyword:

* Use a dictionary or a list; that way you pass around a single variable
that contains modifiable elements.

* Pass around a class instance with the variables inside it.

* Move your functions into a class (making them methods) and then the
variables will be global to the class instance.
--
                      --- Aahz (@netcom.com)

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

Have a *HAPPY* day!!!!!!!!!!



More information about the Python-list mailing list