[mod_python] pass variables between functions.

Scott Sanders sanders at apache.org
Fri Feb 11 13:58:51 EST 2005


It won't work for everyone, if someone's config uses a threaded MPM for 
Apache, or multiple python interpreters.  Then the x global var is only 
global to a particular interpreter.

Scott

On Feb 11, 2005, at 10:49 AM, Chris Jackson wrote:

> [+ mod_python]
>
> I've tested your code.  It works fine the way you have it (add an
> output statement to def test)....
>
> Everytime http://path/to/your/site/test is called, x is reset to 1
> Each time http://path/to/your/site/add is called, x is incremented.
>
> ~= Chris =~
>
> On Fri, 11 Feb 2005 13:28:15 -0500, donnie jones
> <donniejones18 at gmail.com> wrote:
>> I meant retain a value across function, not constants.
>> The global did not work for me..
>>
>> Must I "return" the variable in order for it to be
>> changed for the second function?
>>
>> What kind of sessions issue could be happening?
>> I don't have any sessions declared....
>>
>> Thanks.
>> __
>> Donnie
>>
>>
>> On Fri, 11 Feb 2005 13:25:14 -0500, Chris Jackson
>> <christopher.jackson at gmail.com> wrote:
>>> 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
>>>>
>>>
>>
> _______________________________________________
> 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