[mod_python] pass variables between functions.

Chris Jackson christopher.jackson at gmail.com
Fri Feb 11 13:25:14 EST 2005


If you're looking for setting something like constants, then one
suggestion would be to create a separate config module of a sort :

# config.py
COLOR='blue'
SIZE=10

Then, in your publisher handler file you can import the config module
and call it like so:

from mod_python import apache
import config

def test(req):
 mycolor = config.COLOR + 1
 mysize = config.SIZE +1

def add(req):
 mycolor = config.COLOR +2
 mysize = config.SIZE + 2

However, if by consistency, you mean retain a value across functions,
then using global should work fine.  You may just have an issue with
sessions.  What do  you ultimately want to accomplish?

~= Chris =~


On Fri, 11 Feb 2005 12:48:47 -0500, donnie jones
<donniejones18 at gmail.com> wrote:
> Hello,
> 
> I am wanting to have a variable's data between two functions, but I
> can't seem to find a reasonable way to do this with modpython
> publisher...
> 
> example:
> 
> x=0
> 
> def test(req):
>   global x
>   x = 1
> 
> def add(req):
>   global x
>   x=x+1
>   req.write("%d" % x)
> 
> I would like for x to have a consistent value between the functions...
> I thought maybe doing req.x = 1 might work, but I did not have success
> between the functions, req would not maintain the value.
> 
> Any suggestions would be great.
> Thank you.
> 
> __
> Donnie
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list