Skip to content

Instantly share code, notes, and snippets.

@thinkt4nk
Created August 9, 2011 22:04
Show Gist options
  • Save thinkt4nk/1135320 to your computer and use it in GitHub Desktop.
Save thinkt4nk/1135320 to your computer and use it in GitHub Desktop.
iter and map example
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