Created
February 12, 2012 02:22
-
-
Save manjuraj/1805839 to your computer and use it in GitHub Desktop.
Iterators in Python
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
items = [1, 2, 3] | |
it = iter(items) | |
# prints 1 | |
print it.next() | |
# prints 2 | |
print it.next() | |
# prints 3 | |
print it.next() | |
# raise StopIteration | |
print it.next() | |
# Iteration Protocol | |
# | |
# An inside look at the for statement | |
# for x in obj: | |
# # statements | |
# | |
# Underneath the covers | |
# _iter = iter(obj) # Get iterator object | |
# while 1: | |
# try: | |
# x = _iter.next() # Get next item | |
# except StopIteration: # No more items | |
# break | |
# # statements | |
# | |
# Any object that supports iter() and next() is said to be "iterable". | |
# pending -- http://www.dabeaz.com/generators/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment