Skip to content

Instantly share code, notes, and snippets.

@jeremyBanks
Created August 4, 2008 18:25
Show Gist options
  • Save jeremyBanks/3945 to your computer and use it in GitHub Desktop.
Save jeremyBanks/3945 to your computer and use it in GitHub Desktop.
After reading PEP 342 [2010-01] i goofed around with simple generators, I guess
#!/usr/bin/env python
import sys
def mahgen():
v = "START"
c = 0
while 1:
v = yield (v, c)
c += 1
def main():
gen = mahgen()
print gen.send(None) # == gen.next()
print gen.send("PIE")
print gen.send("HAM")
print gen.send("MORE PIE")
print gen.send("CAKE")
if __name__ == "__main__": sys.exit(main())
('START', 0)
('PIE', 1)
('HAM', 2)
('MORE PIE', 3)
('CAKE', 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment