Method in Methods and Variables

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Feb 3 10:06:34 EST 2003


Thomas Guettler <zopestoller at thomas-guettler.de> wrote in 
news:b1lt3u$om5$07$1 at news.t-online.com:

> The following python code behaves strange:
> 
<code snipped>

No, it behaves as advertised.

Without an assignment to my_int you are accessing the variable from the 
nested scope. With the assignment you are trying to access a local variable 
of the same name before it has been assigned.

If the variable in question had been a global you could use the 'global' 
keyword to indicate that you wanted assignment to update the global. There 
is no equivalent though to assign to a variable in a nested scope (although 
you can mutate one).

> I think putting the integer into a list should work.

Yes, or make it a class instance, or convert the whole thing into a class.

-- 
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