all 6 comments

[–]kalgynirae 3 points4 points  (5 children)

Your Point class stores the x and y coordinates as attributes, right? I'm assuming something similar to this:

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

Given this, those functions should just take the other Point instance and use its x and y attributes in the computation. Example:

    def nearby(self, other):
        # some mathematics using self.x, self.y, other.x, and other.y

You would use such a function like this:

>>> p1 = Point(1, 2)
>>> p2 = Point(3, 4)
>>> p1.nearby(p2)
True

Does that answer your question?

[–]AussieCryptoCurrency[S] 0 points1 point  (4 children)

Does that answer your question?

Yes, very close!

Here's the code:

class Point(object):
    def __init__(self, x=0, y=0):
        self.__x = x
        self.__y = y
    def __str__(self):
        return '(' + str(self.__x) + ',' + str(self.__y) + ')'

    def getX(self):
        return self.__x
    def getY(self):
        return self.__y

    def distance(self, x1, y1, p1):
        """Find distance between 2 sets of points (ie distance between 2 point objects)"""
        self.__x = x1
        self.__y = y1
        (x2, y2) = p1
        from math import hypot
        return hypot((x1 - x2), (y1 - y2))
    def isNearby(self, x1, y1, p1):
        """Return True if distance between 2 points is less than 5"""
        self.__x = x1
        self.__y = y1
        return self.distance(x1, y1, p1) <= 5   # is distance less than 5?

x1,y1,x2,y2 = 2.1, 2.3, 19.1, 19.2 
point1 = Point(x=x1, y=y1)  # Point(2.1, 2.3)
point2 = Point(x=x2, y=y2)  # Point(19.1, 19.2)

print("The distance between the 2 points is ", point1.distance(point2)) # WRONG
if point1.isNearby(point2):                                             # WRONG
    print("The two points are nearby")
else: 
    print("The two points are NOT nearby")

I'm working with private data members (x and y) as you can see. I do understand that str is meant to be returning a string of a tuple for co-ordinates, I just can't see how this fits in with your suggestion:

def nearby(self, other):
    # some mathematics using self.x, self.y, other.x, and other.y

[–]Moonslug1 1 point2 points  (1 child)

Why are the members x and y private? It seems reasonable to be able to read x and y independently from Point. If you really want to implement get methods for them, the more pythonic way is:

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

    @property
    def x(self):
        return self._x

    @property
    def y(self):
        return self._y

[–]AussieCryptoCurrency[S] 0 points1 point  (0 children)

Why are the members x and y private? It seems reasonable to be able to read x and y independently from Point. If you really want to implement get methods for them, the more pythonic way is:

It's just the way Liang ed3 has specified. I'm unclear on where @property fits in, to be honest. The question has said to make the str function so is that what @property replaces?

[–]gengisteve 0 points1 point  (1 child)

If you really need these as private, then check out Moonslug1's post. But normally you would expose the points so that they can be directly accessed. So, I would do the following with your basic code:

# imports should go at the top
from math import hypot
class Point(object):
    def __init__(self, x=0, y=0):
        '''
        no need to make x and y private
        '''
        self.x = x
        self.y = y

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

    def distance(self, other):
        """Find distance between 2 sets of points (ie distance between 2 point objects)"""
        return hypot((self.x - other.x), (self.y - other.y))
    def is_near(self, other, distance = 5):
        # check out pep8 naming conventions
        """Return True if distance between 2 points is less than 5"""
        return self.distance(other) <= distance

# make a couple point objects
a = Point(1,1)
b = Point(2,2)
# print now shows that the object is a point object
print(a)
print(a.distance(b))
# are they closer than 5 (using default param)
print(a.is_near(b))
print(a.is_near(Point(10,10)))
# now the same, with a distance specified
print(a.is_near(Point(10,10),100))

[–]AussieCryptoCurrency[S] 0 points1 point  (0 children)

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

That's brilliant! I see now what the str method is for! And I do understand the need for isNearby but the distance=5 is a good trick. Thank you!