Python 2.7:将类方法传递给另一个函数

时间:2015-07-01 01:41:53

标签: python python-2.7 oop

我用几种方法创建了父类(几何)和子类(Point)。我试图使用两个类中的方法来运行测试函数中的进程。我很难在函数中访问这些方法。我在测试函数中创建了三个点,P1 =(0,3),P2 =( - 3,7)和P3 =( - 3,7),并且想要对它们执行任务。例如,def init (self,x,y):假设返回测试函数中列出的点的x,y坐标。我尝试了以下所有方法:print(test(Point(p1))),print test(p1.Point)和print p1.Point。他们都没有工作。有人可以就如何执行该指示提供指导吗?

我的代码:

class Geometry(object):
    uniqueID = -1
    def __init__(self):
    Geometry.uniqueID += 1
    self.id = Geometry.uniqueID

class Point(Geometry):
    def __init__(self, x, y):
        Geometry.__init__(self)
        self.x = x
        self.y = y

    def __str__(self):
        return '(%.2f, %.2f)' % (int(self.x*100)/100.0, int(self.y*100)/100.0)


    def __eq__(self, point):
        if type(point) is Point:
            if self.x == point.x and self.y == point.y:
            return True
            else:
                return False
        else:
            raise Exception("The input parameter has to be a Point object!")

    def identify(self, other):
        if type(other) is Point:
            if self.id == other.id:
                return True
            else:
                return False
        else:
            raise Exception("The input parameter has to be a Point object!")

    def distance(self, other):
        if type(other) is Point:
            x_square = (other.x - self.x) ** 2
            y_square = (other.y - self.y) ** 2
            d = (x_square + y_square) ** 0.5
            return float(d)
        else:
            raise Exception("The input parameter has to be a Point object!")

    def quadrant(self):
        output = ""
        if self.x > 0 and self.y > 0:
            output = "Quad I"
        elif self.x < 0 and self.y > 0:
            output = "Quad II"
        elif self.x < 0 and self.y < 0:
            output = "Quad III"
        elif self.x > 0 and self.y < 0:
            output = "Quad IV"
        elif self.x == 0:
            output = "Y-axis"
        elif self.y == 0:
            output = "X-axis"
        elif self.x == 0 and self.y == 0:
            output = "Origin"

        return output

def test(p1,p2,p3):
    p1 = Point(0, 3)
    p2 = Point(-3, 7)
    p3 = Point(-3, 7)



def collinear(p1, p2, p3):
    p1x = p1.x
    p1y = p1.y

# m1 = ?
# m2 = ?


def main():
    print (p1.Point)

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:1)

使用__ str __实现,我猜你想在打印对象时显式地显示坐标。在这种情况下,我能够导入您的代码,并发现您的打印逻辑运行良好。

以下是工作代码:

class Geometry(object):
    uniqueID = -1
    def __init__(self):
        Geometry.uniqueID += 1
        self.id = Geometry.uniqueID

class Point(Geometry):
    def __init__(self, x, y):
        Geometry.__init__(self)
        self.x = x
        self.y = y

    def __str__(self):
        return '(%.2f, %.2f)' % (int(self.x*100)/100.0, int(self.y*100)/100.0)


    def __eq__(self, point):
        if type(point) is Point:
            if self.x == point.x and self.y == point.y:
                return True
            else:
                return False
        else:
            raise Exception("The input parameter has to be a Point object!")

    def identify(self, other):
        if type(other) is Point:
            if self.id == other.id:
                return True
            else:
                return False
        else:
            raise Exception("The input parameter has to be a Point object!")

    def distance(self, other):
        if type(other) is Point:
            x_square = (other.x - self.x) ** 2
            y_square = (other.y - self.y) ** 2
            d = (x_square + y_square) ** 0.5
            return float(d)
        else:
            raise Exception("The input parameter has to be a Point object!")

    def quadrant(self):
        output = ""
        if self.x > 0 and self.y > 0:
            output = "Quad I"
        elif self.x < 0 and self.y > 0:
            output = "Quad II"
        elif self.x < 0 and self.y < 0:
            output = "Quad III"
        elif self.x > 0 and self.y < 0:
            output = "Quad IV"
        elif self.x == 0:
            output = "Y-axis"
        elif self.y == 0:
            output = "X-axis"
        elif self.x == 0 and self.y == 0:
            output = "Origin"

        return output

def test():

    p1 = Point(0, 3)
    p2 = Point(-3, 7)
    p3 = Point(-3, 7)

    return p1, p2, p3




def collinear(p1, p2, p3):
    p1x = p1.x
    p1y = p1.y

# m1 = ?
# m2 = ?


if __name__ == "__main__":

    p1, p2, p3= test()

    #Either this 
    print "p1 is " + p1.__str__()
    print "p2 is " + p2.__str__()
    print "p3 is " + p3.__str__()

    print "\n*******************\n"

    #Or this
    print p1
    print p2
    print p3

你能验证吗?您还可以确定点(0.3,0.4)是否是您实际进行打印的函数的本地。

上述示例输出:

p1 is (0.00, 3.00)
p2 is (-3.00, 7.00)
p3 is (-3.00, 7.00)

*******************

(0.00, 3.00)
(-3.00, 7.00)
(-3.00, 7.00)