Skip to content

Instantly share code, notes, and snippets.

@gatspy
Created May 3, 2016 18:21
Show Gist options
  • Save gatspy/98b2d62e58c015b6b6f5cd8a4615764c to your computer and use it in GitHub Desktop.
Save gatspy/98b2d62e58c015b6b6f5cd8a4615764c to your computer and use it in GitHub Desktop.
python -- point
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
@gatspy
Copy link
Author

gatspy commented May 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment