static keyword

Nick Jacobson nicksjacobson at yahoo.com
Thu Apr 29 18:03:24 EDT 2004


Yes, that is excellent.  Thank you very much. :)

The good news: you can reset all your "static" data whenever you want,
by calling foo().  Can't do that in C.

The bad news: I just hope I don't forget and call foo() instead of
g.next().
I would rather the command foo() by default call the next iteration,
and, say, foo().reset() would recall the function from scratch.  But
that's neither here nor there..

"Robert Brewer" <fumanchu at amor.org> wrote in message news:<mailman.113.1083254129.25742.python-list at python.org>...
> Nick Jacobson wrote:
> > I believe the following "static" command would be useful in Python.
> > 
> > def foo():
> >     static i = [10, 11]
> >     static firstcall = True
> >     if firstcall:
> >         print "First pass"
> >         firstcall = False
> >     i[0] += 1
> >     print i[0]
> > foo()
> > foo()
> > 
> > 
> > This would output:
> > 
> > First pass
> > 11
> > 12
> 
> Bah. All these old fogies with their default arg hacks, when Nick
> *clearly* wants a generator:
> 
> >>> def foo():
> ... 	i = 10
> ... 	print "First pass"
> ... 	while True:
> ... 		i += 1
> ... 		yield i
> ... 		
> >>> g = foo()
> >>> g.next()
> First pass
> 11
> >>> g.next()
> 12
> 
> 
> Robert Brewer
> MIS
> Amor Ministries
> fumanchu at amor.org



More information about the Python-list mailing list