[Tutor] design of Point class
bob gailer
bgailer at gmail.com
Mon Aug 23 22:30:53 CEST 2010
I am happier with this:
class PointND(list, object):
def __init__(self, *a_list):
super(PointND, self).__init__(a_list)
self.maxIndex = len(self) - 1
def getSet(ix, attName):
msg = "'%s' object has no attribute '%s'" % (p.__class__.__name__,
attName)
def get(self):
if self.maxIndex < ix:
raise AttributeError, msg
return self[ix]
def set(self, value):
if self.maxIndex < ix:
raise AttributeError, msg
self[ix] = value
return property(get, set)
x = getSet(0, 'x')
y = getSet(1, 'y')
z = getSet(2, 'z')
p = PointND(1,2,3)
assert (p.x, p.y, p.z) == (1, 2, 3)
p.x = 6; p.y = 9; p.z = 5
assert (p.x, p.y, p.z) == (6, 9, 5)
try:
p = PointND(1,2)
p.z = 3
except AttributeError:
print 'Passed all tests'
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list