[Pythonmac-SIG] PythonCGISlave conflict with SMTPlib? (MacOS
9, Pers. Web Sharing)
Just van Rossum
just@letterror.com
Mon, 21 Aug 2000 09:19:29 +0100
At 4:10 PM -0700 20-08-2000, Bob Heeter wrote:
>and then in data_module:
>
> MYDATA = 0
>
> def getdata():
> if ( (not MYDATA) or time_to_update() ): # need to load
>database
> MYDATA = get_cgi_data()
> else:
> return MYDATA
>
>Whenever I tried to run this, I got a Name Error exception on MYDATA,
>at the if statement in the getdata function. I was under the impression
>that the function would be aware of the global variable in the module,
>but that doesn't seem to be the case. [ ... ]
Python's scoping rule (short version ;-): "a variable is local to a
function if it is assgined to *somewhere* in the function. Otherwise it's
global." So your MYDATA = ... line makes MYDATA local to the function. You
need to decalre MYDATA global like so:
MYDATA = 0
def getdata():
global MYDATA
if ( (not MYDATA) or time_to_update() ): # need to load
database
MYDATA = get_cgi_data()
else:
return MYDATA
Just