Home > Mobile >  Pyhton's Class - unsupported operand type(s)
Pyhton's Class - unsupported operand type(s)

Time:12-22

I'm trying to coding a Point class to determine if a 3 given point can make a trainagle.
this is the Class I did:

import math


class Point(object):

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

    def getX(self):
        return int(self.x)

    def getY(self):
        return int(self.y)

    def Distance(self):
        return math.sqrt((self.x ** 2)   (self.y ** 2))

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

    def DistanceFromPoint(self, pX):
        dx = int(self.getX - pX.getX)
        dy = int(self.getY - pX.getY)
        return math.sqrt((dx * dx)   (dy * dy))

    @classmethod
    def FromString(cls, Point_str):
        x, y = Point_str
        return cls(x, y)

and this is my Pyhton file:

from Point import Point


def isTriangle(x1, y1, x2, y2, x3, y3):
    return (y2 - y1) * (x3 - x2) != (y3 - y2) * (x2 - x1)


def isTriangle2(p1, p2, p3):
    d1 = p1.DistanceFromPoint(p2)
    d2 = p1.DistanceFromPoint(p3)
    d3 = p2.DistanceFromPoint(p3)
    if d1   d2 > d3 and d1   d3 > d2 and d2   d3 > d1:
        return True
    else:
        return False


def main():
    p1 = Point(5, 10)
    p2 = Point(7, 10)
    p3 = Point(15, 10)
    print(p1.PointToStr())
    print(p2.PointToStr())
    print(isTriangle(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY()))
    print(isTriangle2(p1, p2, p3))

if __name__ == "__main__":
    main()

when I'm trying to run isTriangle2 I get the following error:
TypeError: unsupported operand type(s) for -: 'method' and 'method'

this is the traceback:

Traceback (most recent call last):
  File "C:\Users\barva\PycharmProjects\Giraffe\Ariel-notebook\lec_7 8.py", line 28, in <module>
    main()
  File "C:\Users\barva\PycharmProjects\Giraffe\Ariel-notebook\lec_7 8.py", line 25, in main
    print(isTriangle2(p1, p2, p3))
  File "C:\Users\barva\PycharmProjects\Giraffe\Ariel-notebook\lec_7 8.py", line 9, in isTriangle2
    d1 = p1.DistanceFromPoint(p2)
  File "C:\Users\barva\PycharmProjects\Giraffe\Ariel-notebook\Point.py", line 23, in DistanceFromPoint
    dx = int(self.getX - pX.getX)

At first I tought isTriangle2 didnt transfer the given point to int when I used the getX() and getY() function so I did try to cast them but that didnt help as well.
than I tried to change the class a bit, I think the error is coming from DistanceFromPoint function in the Point class but I dont know how to fix that

CodePudding user response:

in

    dx = int(self.getX - pX.getX)
    dy = int(self.getY - pX.getY)

The getters are functions and need to be called like functions (i.e add () when the getters are called)

CodePudding user response:

One solution would be to use the @property decorator in the methods getX and getY:

@property
def getX(self):
    return int(self.x)

@property
def getY(self):
    return int(self.y)

This way the functions getX and getY will become properties of the class, so you will need to remove the "()" when using them:

 print(isTriangle(p1.getX, p1.getY, p2.getX, p2.getY, p3.getX, p3.getY))
  • Related