接收其类对象的python类方法

时间:2019-06-23 09:51:03

标签: python class-method

我想创建一个Square类,该类具有计算与另一个Square的距离的方法。这是我的定义方式:

class Square:
    def __init__(self, _x: int, _y: int):
        self.x = _x
        self.y = _y

    def distance(self, _other_square: Square) -> int:
        pass

_other_squareSquare类型的对象。 这给了我一个未解决的参考“平方”错误。

有办法解决吗?

1 个答案:

答案 0 :(得分:1)

将函数定义更改为此:

def distance(self, _other_square: 'Square') -> int:
        pass

类型提示现在是一个str实例,将在模块装入后解析,因此已定义了Square类型。 See here for all details.