global declaration from within functions

Jeff Shannon jeff at ccvcorp.com
Wed Aug 15 16:30:56 EDT 2001


Rob Andrews wrote:

> I think I've got a bare grasp of the metaphysics involved in global
> value assignment in Python. I was really asking more of a pragmatic
> question along the lines of why one would really want to change the
> value assigned to a global.
>
> I imagine it might be handy for a variety of different hacks one might
> use for convenience, but I wonder doing it is actually good practice.
>

Well, typically the use of global variables *is* considered to be poor, hackish
programming practice.  But if you *are* using globals, it would be almost
pointless to have them be unchangeable.  Yes, you can usefully create constants
this way, but globals should not be limited to constants--if you're going to have
them at all, they should be real variables.  A common (not necessarily good...)
use of a global variable, would be to maintain a count of some sort, where the
count might change in any number of different, unrelated functions.  You *could*
find other ways of doing this, but a global variable is often the most convenient
(though not safest).

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list