Created
November 28, 2012 00:15
-
-
Save durden/4158116 to your computer and use it in GitHub Desktop.
Python generator attempting to simulate static variable in a C function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just playing around and thinking about how itertools.count might work (without # looking at source :) ) | |
# Simple version of a 'static' variable in C, just keep state internally | |
# and use next() to get values out | |
def counter(): | |
for ii in xrange(100): | |
yield ii | |
cnt = counter() | |
print cnt.next() | |
print cnt.next() | |
# Equivalent of this but built-in count doesn't have a max defined explicitly | |
from itertools import count | |
cnt = count(0) | |
print cnt.next() | |
print cnt.next() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def counter():
i = 0
while True:
i += 1
yield i
Nice brain teaser.