Help passing variables.

Christopher T King squirrel at WPI.EDU
Wed Aug 18 10:08:41 EDT 2004


On 18 Aug 2004, aToaster wrote:

>  I started off by trying to make a simple calculator program and I
> need to pass the calcTotal variable in and out of the ButtonHandler
> function, how do I do this?

The quick answer is that in buttonHandler_a(), you have to decalre 
calcTotal global; otherwise it will assume it's a local value:

     def buttonHandler_a(self, argument1, argument2):
             global calcTotal
             print "You have clicked :", argument2
             calcTotal = calcTotal +argument1
             print calcTotal

The long answer is that anytime you think you need a global variable, you 
probably don't.  The better way to go about this is to have calcTotal be a 
property of CalcApp ("self.calcTotal = 0" in CalcApp.__init__()).  This 
way, CalcApp will be re-entrant, in the case that you ever need to 
instantiate more than one of CalcApp (something you probably wouldn't do 
with an application like that, but a good practice nonetheless).






More information about the Python-list mailing list