简体   繁体   中英

Calculating the distance between two points in A Point Class Python

I am struggling to understand the idea of object orientated programming in python. I am currently trying to calculate the Euclidean distance between 2 points using a Point class

import math

class Point(object):
    """A 2D point in the cartesian plane"""

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

    def __repr__(self):
        return 'Point({}, {})'.format(self._x, self._y)

    def dist_to_point(self, Point):
        dist = math.sqrt((self._x - Point.x())**2 + (self._y - Point.y())**2)
        return dist

I know that dist_to_point method is wrong because python is returning:

Test Result: 'Point' object has no attribute 'x'

I am struggling to understand how the referencing works? I defined Point as in a Point object, why can't I use this?

Also whats up with the .self? If I want to use the x and y coordinate of a point under the Point class, I have to call self._x and self._y?

import math

class Point(object):
    """A 2D point in the cartesian plane"""

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

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

    def dist_to_point(self, Point):
        dist = math.sqrt((self.x - Point.x)**2 + (self.y - Point.y)**2)
        return dist

p1 = Point(4,9)
p2 = Point(10,5)
print(p1.dist_to_point(p2))

>> 7.211102550927978

self is the object instance
"_" before a variable means it's private by convention (so not applicable here)
no "()" after x & y

You've declared self._x so you can use it, but you haven't declared second point. Declare it first. It is better that you add two more parameters in the __init__() and edit first parameters as x1 y1 and added parameters x2 y2. Then initialise them as self.x1=x1 self.y1=y1 self.x2=x2 self.y2=y2 . Then change the dist_to_point() method as this:

def dist_to_point(self):
      return math.sqrt((self.x1-self.x2)**2+(self.y1-self.y2)**2)

In Python using an underscore before a class method or variable is a convention to indicate it's private and should not be used outside the class. A solution for your problem could be making x and y public variables of Point class, this way you can access them outside the class. Here is an example:

import math


class Point:
    """A 2D point in the cartesian plane"""

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

    def __repr__(self):
        return 'Point({}, {})'.format(self._x, self._y)

    def dist_to_point(self, Point):
        dist = math.sqrt((self.x - Point.x)**2 + (self.y - Point.y)**2)
        return dist


p1 = Point(0, 0)
p2 = Point(1, 1)

distance = p1.dist_to_point(p2)

print(distance)

Making these values public may not always be the best solution, but for this simple case it's ok

And you were also putting () after class variable access :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM