Created
September 15, 2017 20:57
-
-
Save mrdrozdov/09408b7f95d84a5d03c6543f13ffed6b to your computer and use it in GitHub Desktop.
CustomRange (range iterator + len)
This file contains hidden or 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 CustomRange: | |
def __init__(self, max): | |
self.max = max | |
def __iter__(self): | |
self.curr = 0 | |
return self | |
def __len__(self): | |
return self.max | |
def next(self): | |
numb = self.curr | |
if self.curr >= self.max: | |
raise StopIteration | |
self.curr += 1 | |
return numb | |
if __name__ == '__main__': | |
index = CustomRange(10) | |
print(len(index)) | |
for i in index: | |
print(i) |
This file contains hidden or 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 CustomRange: | |
def __init__(self, max): | |
self.max = max | |
def __iter__(self): | |
self.curr = 0 | |
return self | |
def __len__(self): | |
return self.max | |
def __next__(self): | |
numb = self.curr | |
if self.curr >= self.max: | |
raise StopIteration | |
self.curr += 1 | |
return numb | |
if __name__ == '__main__': | |
index = CustomRange(10) | |
print(len(index)) | |
for i in index: | |
print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More info on generators for anyone interested: http://intermediatepythonista.com/python-generators