How to delete/mainpulate global variables ??

Jeff Shannon jeff at ccvcorp.com
Fri Mar 22 13:57:19 EST 2002


Berenike Loos wrote:

> I am using two function in my program. The first one reads
> out some text of an TK entry field and puts it into a
> global variable. The other function has to read out the
> global variable and has to print out the text. Now the
> global variable should be deleted, so if I call the second
> function again without calling the first, NO text will be
> displayed. thx a lot

Well, first off, I'd point out that global variables are
usually not the best way to do things.  There's a fair
chance in any nontrivial program that they will be changed
in ways (or at times) that you don't expect them to.  But
the short answer to your question, is to have the second
function set the variable equal to None.

Var1 = None

def Func1():
    global Var1
    Var1 = GetValueFromWidget()

def Func2():
    global Var1
    if Var1 is not None:
        print Var1
        Var1 = None

Jeff Shannon
Technician/Programmer
Credit International








More information about the Python-list mailing list