Mercurial > hg > config
comparison python/example/iterable.py @ 587:e10d85ee0be3
dammit
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 23 Jan 2014 16:16:24 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
586:e14ba4fbc0e7 | 587:e10d85ee0be3 |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 illustration of a class iterable | |
6 """ | |
7 # XXX does not work!!@ TODO!!! XXX # | |
8 | |
9 import argparse | |
10 import sys | |
11 | |
12 class MyIterable(object): | |
13 def __init__(self, max): | |
14 self.items = list(range(max)) | |
15 def __iter__(self): | |
16 return self | |
17 def next(self): | |
18 for i in self.items: | |
19 yield i | |
20 | |
21 def main(args=sys.argv[1:]): | |
22 | |
23 usage = '%prog [options]' | |
24 parser = argparse.ArgumentParser(usage=usage, description=__doc__) | |
25 options = parser.parse_args(args) | |
26 | |
27 myiter = MyIterable(10) | |
28 for i in myiter: | |
29 print ('Hi {}'.format(i)) | |
30 | |
31 if __name__ == '__main__': | |
32 main() |