static keyword

Rick Ratzel rick.ratzel at magma-da.com
Thu Apr 29 12:31:33 EDT 2004


    Robert beat me to it...I was going to reply with a generator 
example!  Indeed...Nick *clearly* wants a generator.  By far the most 
elegant IMO.

-Rick

Robert Brewer wrote:
> 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