Nested scopes question

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Jul 22 04:32:30 EDT 2002


Fernando Perez <fperez528 at yahoo.com> wrote in
news:ah84gq$ave$1 at peabody.colorado.edu: 

> Thanks a lot for the references. So I guess my uglyish hack of hiding
> the value inside a list is after all the solution for this problem.
> Well, at least it gets the job done :)

It sounds a lot as though you should be considering using a class here. 
Classes are designed for just this job (sharing changeable state between 
separate function calls).

>>> class C:
    def bar(self):
        print "x=",self.x
        self.x += 1

    def foo(self):
        self.x = 0
        for i in range(5):
            self.bar()

            
>>> foo = C().foo
>>> foo()
x= 0
x= 1
x= 2
x= 3
x= 4

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