Created
May 3, 2016 18:21
-
-
Save gatspy/98b2d62e58c015b6b6f5cd8a4615764c to your computer and use it in GitHub Desktop.
python -- point
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 Point(object): | |
__slots__ = ('x', 'y') | |
def __init__(self, x=0, y=0): | |
self.x = x | |
self.y = y | |
def __setattr__(self, name, value): | |
# print self, name, value | |
return super(Point, self).__setattr__(name, value) | |
def __getitem__(self, index): | |
return self.__getattribute__(self.__slots__[index]) | |
def __setitem__(self, index, value): | |
self.__setattr__(self.__slots__[index], value) | |
def __len__(self): | |
return 2 | |
def __iter__(self): | |
return iter([self.x, self.y]) | |
p = Point() | |
p[0] = 2 | |
print len(p) | |
for pp in p: | |
print pp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python for Point(x,y) class