View Single Post
  #6 (permalink)  
Old 01-15-2009, 04:17 PM
Sion Arrowsmith
Guest
 
Posts: n/a
Default Re: point class help

r <rt8396@gmail.com> wrote:
>here is what i have, it would seem stupid to use a conditional in each
>method like this...
>
>def method(self, other):
> if isinstance(other, Point2d):
> x, y = origin.x, origin.y
> else:
> x, y = origin[0], origin[1]
> #modify self.x & self.y with x&y


Here's another alternative (I'd've gone with the subclassing tuple if
writing this from scratch):

class Point2d(object):
...
def __getitem__(self, i):
if not 0 <= i <= 1:
raise IndexError
return getattr(self, ['x', 'y'][i])

def method(self, other):
x, y = other[0], other[1]
...

--
\S -- siona@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Reply With Quote