Created
August 9, 2011 22:04
-
-
Save thinkt4nk/1135320 to your computer and use it in GitHub Desktop.
iter and map example
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 myClass(object): | |
def __init__(self): | |
self.elems = ['meh','meep','moop','mop'] | |
self.index = None | |
def __iter__(self): | |
return self | |
def next(self): | |
if self.index is None: | |
self.index = 0 | |
return self.elems[self.index] | |
if len(self.elems) > (self.index + 1): | |
self.index += 1 | |
return self.elems[self.index] | |
else: | |
self.index = None | |
raise StopIteration | |
o = myClass() | |
a = map(lambda x: '#' + x ,o) | |
for i in a: | |
print i | |
## yields | |
#meh | |
#meep | |
#moop | |
#mop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment