Created
June 17, 2019 07:25
-
-
Save jnpn/4e9ce379ae7b5f7ed8b258b4b7fcade2 to your computer and use it in GitHub Desktop.
callable generator
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
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