1

I'm still new to this site, so forgive poor formatting. I'm trying to make a class for a point on a cartesian plane and a method for finding the slope of a line when given two points. However, while working on it, pycharm tells me that Instance attribute slope(name of variable) defined outside __init__. However when I place self.slope into __init__ and assign it to run it gives the following error:

Traceback (most recent call last):
  File "/Volumes/SEAGATE_for_editing/Programming/Python/graphing stuff/stuff.py", line 28, in <module>
    print(point1.slope(point2))
TypeError: 'NoneType' object is not callable

How do I fix this? Here is my code. Any help is greatly appreciated.

class Point:

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

        self.y_slope = None
        self.x_slope = None
        self.slope = None

    def slope(self, other):
        self.y_slope = other.y - self.y
        self.x_slope = other.x - self.x
        self.slope = self.y_slope / self.x_slope
        return self.slope

    def dist(self, other):
        self.x_dist = other.x - self.x
        self.y_dist = other.y - self.y
        self.distance = ((self.x_dist ** 2) + (self.y_dist ** 2)) ** (1/2)
        return self.distance


point1 = Point(-2, -5)
point2 = Point(1, 1)
print(point1.slope(point2))
print(round(point1.dist(point2), 4))
0

3 Answers 3

4

point1.slope is None. You set it to None yourself in __init__:

class Point:
    def __init__(self, x, y):
        self.x = x
        ...
        self.slope = None  # here

You don't really need self.slope here. You can return the result of division directly:

def slope(self, other):
    y_slope = other.y - self.y
    x_slope = other.x - self.x
    return y_slope / x_slope

There's also no need for self.x_slope and self.y_slope as you can use regular local variables.


This happens because Python is dynamically typed. You can also do print = lambda *args, **kwargs: "hacked!", and now you can't print anything (__builtins__.print to the rescue!).

Sign up to request clarification or add additional context in comments.

Comments

3

You're using the same name for an attribute self.slope and your method def slope(self, other). The attribute shadows the method name, so when you try calling point1.slope(...), it tries to call the value of the attribute which is initialized to None.

Pick different names for the two things and your code will work as expected.

Or, as ForceBru noticed in their answer, just don't bother with any of the slope or dist related attributes at all, the seem to be unneeded. You can use local variables in your methods instead, just name them without the slope. prefix.

Comments

1

You have an issue in your slope method. You have a method name of slope and you are also setting a self.slope with the output, that is causing the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.