point class help

Sion Arrowsmith siona at chiark.greenend.org.uk
Thu Jan 15 12:17:03 EST 2009


r  <rt8396 at 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 at 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



More information about the Python-list mailing list