我正在尝试使用类型提示(Python中的新功能)来使代码更具可读性:
class Point:
def __init__(self, x: float, y:float):
self.x = x
self.y = y
def is_in_rectangle(self, rectangle: Rectangle):
if rectangle.lower_left.x < self.x < rectangle.upper_right.x \
and rectangle.lower_left.y < self.y < rectangle.upper_right.y:
return True
else:
return False
class Rectangle:
def __init__(self, lower_left: Point, upper_right: Point):
self.lower_left = lower_left
self.upper_right = upper_right
执行该代码时,我得到:
NameError Traceback (most recent call last)
<ipython-input-1-c01b4ce97fcf> in <module>
----> 1 class Point:
2
3 def __init__(self, x: float, y:float):
4 self.x = x
5 self.y = y
<ipython-input-1-c01b4ce97fcf> in Point()
5 self.y = y
6
----> 7 def is_in_rectangle(self, rectangle: Rectangle):
8 if rectangle.lower_left.x < self.x < rectangle.upper_right.x \
9 and rectangle.lower_left.y < self.y < rectangle.upper_right.y:
NameError: name 'Rectangle' is not defined
我该如何解决?
如果我在Point上定义Rectangle,那么我会得到一个NameError: name 'Point' is not defined
。