【问题标题】:Python 2.7: Passing class methods to another functionPython 2.7:将类方法传递给另一个函数
【发布时间】:2015-09-18 00:24:35
【问题描述】:

我用几种方法创建了一个父类(几何)和子类(点)。我正在尝试使用这两个类中的方法在测试函数中运行进程。我很难在函数中访问这些方法。我在测试函数中创建了三个点,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()

【问题讨论】:

    标签: python python-2.7 oop


    【解决方案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
    

    你能验证一下吗?您还可以确定 Point(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)
    

    【讨论】:

    • 所以p1坐标必须在main函数中列出来?我试图创建一个测试函数,其中列出了该函数下的三个点,P1 = (0,3)、P2 = (-3,7) 和 P3 = (-3,7),并使用 main 打印这些坐标功能。我希望我的输出返回 Coordinates = (0, 3)
    • 所以 p1、p2 和 p3 具有函数 test() 的本地范围,这意味着像 main() 这样的函数不知道 p1 、 p2、p3 的值是什么。我已经编辑了我的解决方案。
    • 你能再检查一下吗!我刚刚在最后一分钟进行了编辑,所以我猜想在堆栈溢出服务器之间保持一致需要一些时间
    猜你喜欢
    • 2015-07-07
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    相关资源
    最近更新 更多