Skip to content

Instantly share code, notes, and snippets.

@jnpn
Created June 17, 2019 07:25
Show Gist options
  • Save jnpn/4e9ce379ae7b5f7ed8b258b4b7fcade2 to your computer and use it in GitHub Desktop.
Save jnpn/4e9ce379ae7b5f7ed8b258b4b7fcade2 to your computer and use it in GitHub Desktop.
callable generator
class Gun:
'''
>>> g = Gun()
>>> f = g()
>>> [next(f), next(g)]
[2, 1]
>>> [next(f), next(g)]
[3, 2]
>>> [next(f), next(g)]
[4, 3]
>>> [next(f), next(g)]
[5, 4]
'''
def __init__(self, start=0):
self.start = start
def __call__(self):
return Gun(self.start+1)
def __next__(self):
self.start += 1
return self.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment