Skip to content

Instantly share code, notes, and snippets.

@nickcharlton
Created April 12, 2013 17:51
Show Gist options
  • Save nickcharlton/5373826 to your computer and use it in GitHub Desktop.
Save nickcharlton/5373826 to your computer and use it in GitHub Desktop.
A Python iterable that returns one of two member variables.
class Vector():
def __init__(self, x, y):
self.x = x
self.y = y
self.index = 0
def __iter__(self):
return self
def next(self):
if self.index > 1:
raise StopIteration
self.index = self.index + 1
if self.index == 1:
return self.x
else:
return self.y
v = Vector(5, 6)
x, y = v
print "x: %d, y: %d" % (x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment