python asp page keeps state across queries !?!?

N

nicolas_riesch

I am writing asp pages in python, running on IIS 5 on Windows.

I notice that variables outside functions keep their value across
queries.
I don't know if it is normal.

Here is a little script that demonstrates it:

------------------ problem.asp ---------------------

<%@LANGUAGE=Python%>

<%

try:
if x == 0: # if x is not defined, an exception is raised here and
code goes to the except clause
pass
else:
x = x + 1 # if x exists, increment x
except:
x = 10 # if x not defined, initialize x to 10

Response.write('Hello, the value of x is ' + str(x) ) # write value
of x

%>
-----------------------------------------------------

The first time I fetch this asp page from the web browser, I get:

Hello, the value of x is 10

So far, it works.
But then, when I click on the refresh button to query the asp page
again, I get:

Hello, the value of x is 11

and then

Hello, the value of x is 12

and so on ...

Each time I reload this asp page, the x variable is incremented !!!

It seems that IIS loads this asp page like a module and keep it in
memory.
That's why all variables at the module level act as global variables
that survives to multiple queries.

To be sure, I tried a similar asp script written in VB script, and I
can see that in VBscript, variables at this same level ARE NOT kept
across different queries !!!
So, the behavior of Python asp page is different from the same page
written in VBscript.

I don't know if it is a normal thing or if it is a bug, of if I have
missed something.
Someone has an explanation ?
 
S

Sybren Stuvel

nicolas_riesch enlightened us with:
I notice that variables outside functions keep their value across
queries. I don't know if it is normal.

I think it's normal.
To be sure, I tried a similar asp script written in VB script, and I
can see that in VBscript, variables at this same level ARE NOT kept
across different queries !!!
So?

Someone has an explanation ?

Speed. No need to reload the module for each query. Just don't use any
uninitialized module-scope variables. It's bad style anyway.

Sybren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top