How to set local variables in a method

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Jan 29 15:11:24 CET 2002


tommy.skeide at rolls-royce.com (TommyS) wrote in 
news:52132307.0201290516.5dbf3c96 at posting.google.com:

> A beginners question...
> I intend to set the variables a and b in the getvalues method.. Does
> anyone know how to do this??
> 
> class test:
>       def calculate(self):
>            a,b = 0,0
>            z = # what to put here???
answer: nothing. You cannot modify the callers variables. Instead you 
should return the values you want to set as results from your function and 
assign to the locals when you call the function:
>            self.getvalues(z)
            a, b = self.getvalues()
>            print a + b 

>       def getvalues(self,z):
>            z['a'] = 2
       def getvalues(self):
            return 2, 3

> Thanks a lot!
> 



-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list