本文介绍了在Python 3中使用魔术方法计算欧几里得距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以为我计算欧几里得距离:

I have a code that calculates Euclidean distance for me:

class Point:
    """A point in two-dimensional space."""

    def __init__(self, x, y):
        self._x = x
        self._y = y

    def __eq__(self, other):
        return self._x == other._x and self._y == other._y

    def distance(self, other):
        new_x = self._x - other._x
        new_y = self._y - other._y
        print(new_x,'  ',new_y)
        return (new_x ** 2 + new_y ** 2) ** 0.5

p1 = Point(10, 4)
p2 = Point(3, 1)


print('Euclidean distance : 'p1.distance(p2))

但是,现在我想使用python中的魔术方法(如__sub____pow__)计算该距离.我已经成功实现了__sub__,但是我不知道如何实现__pow__和平方根.到目前为止,这是我的代码:

However, now I want to calculate this distance using magic methods in python like __sub__ and __pow__. I've managed to implement __sub__ but I don't know how to implement for __pow__ and square root. This is my code so far:

class Point_1(object):

    def __init__(self, x, y):
        self._x = x
        self._y = y


    def setX(self, x,y):
        self._x = x
        self._y = y

    def getX(self):
        return self._x,self._y


    def __sub__ (self, other ):
        return Point_1(self._x - other._x, self._y - other._y)

    def __pow__(self,p):
        return Point_1(self._x ** p, self._y **p)




p1 = Point_1(10,4)
print(p1.getX())

p2 = Point_1(3,1)
print(p2.getX())

p3 = p1 - p2

如何使用魔术方法实现公式的其余部分.我真的很困惑帮助我将不胜感激.

How can I implement the rest of the formula using magic methods. I'm really confused. Help would me appreciated.

推荐答案

如上所述,使用Point类表示向量可能不是一个好主意.在简单的程序中没关系,但是在更复杂的代码中可能会造成混淆.通常的做法是使积分不变.但是无论如何...

As has been mentioned, it may not be a good idea to use a Point class to represent vectors. It's ok in simple programs, but it can get confusing in more complex code. The usual practice is to make points immutable. But anyway...

要执行此欧几里得距离运算,我们可以滥用"新的 __matmul__ 魔术方法.此方法由@运算符调用.这是一个基于您的代码的简短演示.请注意,我使用xy作为属性,没有充分的理由将它们标记为私有.

To do this Euclidean distance operation we can "abuse" the new __matmul__ magic method. This method is invoked by the @ operator. Here's a short demo based on your code. Notice that I'm using x and y as the attributes, there's no good reason to mark them as private.

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

    def __repr__(self):
        return "Point({}, {})".format(self.x, self.y)

    def __add__ (self, other ):
        return Point(self.x + other.x, self.y + other.y)

    def __sub__ (self, other ):
        return Point(self.x - other.x, self.y - other.y)

    def __pow__(self, p):
        return Point(self.x ** p, self.y **p)

    def __abs__(self):
        d = self ** 2
        return (d.x + d.y) ** 0.5

    def __matmul__(self, other):
        ''' Euclidean distance between self & other '''
        return abs(self - other)

# Test

a = Point(5, 6)
b = Point(2, 2)
print(a + b)
print(a - b)
print(a @ b)

输出

Point(7, 8)
Point(3, 4)
5.0

这篇关于在Python 3中使用魔术方法计算欧几里得距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:49