Passing object of same class as argument to a member function

Hi All,
How to pass object of same class as argument to a member function? For example,

class Point:
    def __new__(self, x, y):
        self.X = x
        self.y = y
    
    def AngleTo(self, point : Point):
        x1 = self.X
        y1 = self.Y
        x2 = point.X
        y2 = point.Y
        if (x1 ==  x2):
            return None
        else:
            return (y2-y2) / (x2-x1)

Note that thee function AngleTo takes an instance of Point itself as argument

1 Like

This works perfectly fine, the issue is that when you’re defining the function here, the class itself hasn’t been defined yet so Point isn’t a valid name. You can either quote it - point: 'Point' - which type checkers understand, or use

from __future__ import annotations

at the start of your module to opt into making all annotations strings to avoid it everywhere.

1 Like

@TeamSpen210
Thanks a lot… That worked…!
Here is the final code

from __future__ import annotations
import math
import random

class Point:
    def __init__(self, x, y):
        self.X = x
        self.Y = y
    def __str__(self):
    	return f"{(self.X, self.Y)}"
    
    def AngleTo(self, point : Point):
        x1 = self.X
        y1 = self.Y
        x2 = point.X
        y2 = point.Y
        if x2 == x1:
            if y2 < y1:
            	return 0
            elif y2 > y1:
            	return 90
            else:
            	return None
        else:
            deltaX = (x2 - x1)
            deltaY = (y2 - y1)
            angle = math.atan2(deltaY, deltaX)* 180 / math.pi
            return angle
1 Like

hey can you explain, where you define “point” object