point class help

r rt8396 at gmail.com
Wed Jan 14 11:31:07 EST 2009


before anybody say's anything, i screwed up when i pasted the code,
here is what i really have...

def method(self, other):
    if isinstance(other, Point2d):
        x, y = other.x, other.y
    else:
        x, y = other[0], other[1]
    return self.x+x, self.y+y


#and the fixed class :)
class Point2d():
    def __init__(self, x, y=None):
        if type(x) == tuple:
            self.x = x[0]
            self.y = x[1]
        else:
            self.x = x
            self.y = y
    def __str__(self):
        return 'Point2d(%f, %f)' %(self.x, self.y)
    def __add__(self, other):
        if isinstance(other, Point2d):
            x, y = other.x, other.y
        else:
            x, y = other[0], other[1]
        return (self.x+x, self.y+y)
    def __sub__(self, other):
        pass
    def __iadd__(self, other): #+=
        pass
    def __isub__(self, other): #-=
        pass

PS: i know i could have one method like

def get_xy(self, other):
    #conditional

and then call it like

x, y = self.get_xy(other)

but that seems pretty clumsy. There must be a more Pythonic way!



More information about the Python-list mailing list