static keyword

Robert Brewer fumanchu at amor.org
Thu Apr 29 11:52:47 EDT 2004


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