0

I created a class that takes 2 coordinates and returns the slope and distance of the respective coordinates. It works fine but, apparently, what I did was wrong and was supposed to give an error.

My code:

class Line:
    def __init__(self, coord1, coord2):

        self.coord1 = coord1
        self.coord2 = coord2

    def distance(self):

        return math.sqrt( ((self.coord1[0]-self.coord2[0])**2)+((self.coord1[1]-self.coord2[1])**2) )

    def slope(self):

        c = self.coord2[0]-self.coord1[0]
        d = self.coord2[1]-self.coord1[1]
        if d==0:
            return "zero"
        elif c==0:
            return "Infinity"
        else:
            return d/c

line1=Line((8,3),(0,-4))
print(line1.distance())
print(line1.slope())
RESTART: C:\Users.....\Classes.py
10.63014581273465 **The result is right...?**
0.875

I get the right output, but this is how I am supposed to do it:

>>> line1=Line((8,3),(0,-4))
>>> line1.distance()
Traceback (most recent call last):File "<stdin>", line
1, in <module>TypeError: 'float' object is not callable  **This is like mine but error?**
>>> line1.slope()
Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError:
    'float' object is not callable **Just like mine but error?**
>>> line1.distance
10.63
>>> line1.slope
0.875

What is the difference and is mine wrong?

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
  • make distance a variable, not a function – Mohammad Athar Jun 07 '18 at 16:41
  • 1
    Related reading: https://docs.python.org/3/library/functions.html#property – Kevin Jun 07 '18 at 16:43
  • This may sound real stupid but im a beginner ....but how do i do that? – Sheshank Ps Jun 07 '18 at 16:43
  • As suggested by @Kevin, checkout the documentation. You can also just search a bit for `python`, `property`, `decorator`. There are multiple threads around here as well, like [this](https://stackoverflow.com/questions/6304040/real-world-example-about-how-to-use-property-feature-in-python). – Ondrej K. Jun 07 '18 at 20:02

2 Answers2

2

just put property decorator behind the func some useful docs

@property
def distance(self):
    ...

@property
def slope(self):
    ...
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
  • Thanks a lot this helped!!! But why? I really don't understand why? What is a property decorator? – Sheshank Ps Jun 07 '18 at 16:46
  • 1
    A property is an application of the descriptor protocol. Put briefly, `line1.distance` doesn't return the value of an instance attribute, but the return value of a call to `Line.distance.__get__(line1, Line)` – chepner Jun 07 '18 at 16:49
-2

you need distance/slope to be variables

class Line: 

def __init__(self, coord1, coord2):

    self.coord1 = coord1
    self.coord2 = coord2
    self.distance = self.get_distance()
    self.slope = self.get_slope()

def get_distance(self):

    return math.sqrt( ((self.coord1[0]-self.coord2[0])**2)+((self.coord1[1]-self.coord2[1])**2) )



def get_slope(self):

    c = self.coord2[0]-self.coord1[0]
    d = self.coord2[1]-self.coord1[1]
    if d==0:
        return "zero"
    elif c==0:
        return "Infinity"
    else:
        return d/c

This way, distance/slope are floats, and not functions; so you'll get the error that you're after

Mohammad Athar
  • 1,953
  • 1
  • 15
  • 31
  • Thank you! But below is simpler and worked exactly like it should. – Sheshank Ps Jun 07 '18 at 16:47
  • it's "simpler" but you don't understand it. Decorators are a bit more advanced than what I showed – Mohammad Athar Jun 07 '18 at 16:49
  • This is unlikely to be what the assignment had in mind. In particular, you'll need to manually call `get_distance` and `get_slope` manually each time you change one of the coordinates. – chepner Jun 07 '18 at 17:00