Created
August 23, 2012 02:29
-
-
Save yyl/3431519 to your computer and use it in GitHub Desktop.
iterates
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
a = [1,2,3] | |
i = iter(a) | |
i.next() |
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
# iterator | |
for i in [1,2,3]: | |
print i*i | |
# generator | |
def square(li): | |
for i in xrange(1, li): | |
yield i*i | |
for i in square(3): | |
print i | |
''' | |
actually this is not a good example because code for generator | |
is much more than that of iterator. | |
but ... | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment