Created
April 12, 2013 17:51
-
-
Save nickcharlton/5373826 to your computer and use it in GitHub Desktop.
A Python iterable that returns one of two member variables.
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 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