[Tutor] Not able to store result between function call.. Help needed

Bob Gailer bgailer at alum.rpi.edu
Mon Nov 13 16:31:04 CET 2006


Asrarahmed Kadri wrote:
>  
>  
>  
> Hi Folks,
>  
>  
> I have a textbox and a button (in tkinter). What I want is the 
> following functionality: The user enters a number in the text box and 
> presses the button. The event will call a function called "adder" and 
> when the user enters another value and presses the button, the 'adder' 
> function is again called and the summation of both the numbers is 
> displayed.
> The problem is I am not able to store the value of 'result' variable. 
> I want to make it persistant between the function calls.
There are at least 3 ways to accomplish persistent storage in Python:
global variables
class attributes
files

For your purpose the simplest is a global variable (one that may be 
assigned a value outside of any function, and/or may be (re)assigned 
within a function if declared global in the function.

Simplest demonstration:
def f():
  global a
  a = 3
f()
print a
>  
> TIA.
> Regards,
> Asrarahmed
>
> -- 
> To HIM you shall return.
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list