【发布时间】:2021-07-26 12:39:38
【问题描述】:
以下源代码中的错误是什么?
我自己找不到。
ShapeBase.py
from abc import ABC
class ShapeBase(ABC):
def __init__(self, idd: str):
self.id_: str = idd
self.cx_: float = 0.0
self.cy_: float = 0.0
@property
def cx_(self) -> float:
return self.cx_
@cx_.setter
def cx_(self, cx: float):
self.cx_ = cx
@property
def cy_(self) -> float:
return self.cy_
@cy_.setter
def cy_(self, cy: float):
self.cy_ = cy
def id(self) -> str:
return self.id_
def area(self) -> float:
pass
Square.py
from shapes.ShapeBase import ShapeBase
class Square(ShapeBase):
def __init__(self, idd: str, a: float):
super().__init__(idd)
self.a_ = a
def area(self) -> float:
return self.a_ * self.a_
def width(self) -> float:
return self.a_
main.py
from shapes.Square import Square
if __name__ == '__main__':
s1 = Square('S1', 4.0)
print("Area = " + str(s1.area()))
输出
C:\Users\pc\AppData\Local\Microsoft\WindowsApps\python3.7.exe C:/Users/pc/source/repos/Shapes/main.py
Process finished with exit code -1073741571 (0xC00000FD)
顺便说一句,这个问题与属性的名称无关。
这个问题与继承有关。
【问题讨论】:
-
重复的含义是你已经结束了一个无限循环的递归。尝试找出某个东西在哪里调用自己(也许通过调用其他最终调用原始对象的东西)。
-
尝试在 pycharm 中重新配置您的 python 解释器并将其设置为 python.exe。您可以通过使用
where python命令获取python.exe 的位置 -
我遇到了另一个异常,
E RecursionError: maximum recursion depth exceeded !!! Recursion detected (same locals & position)。in __init__ super().__init__(idd) in __init__ self.cx_: float = 0.0 in cx_ self.cx_ = cx in cx_ self.cx_ = cx -
所以从@Guy 的评论来看,你有一个问题,
self.cx_最终一遍又一遍地调用自己。 -
问题出在 setter 和 getter 上。当在 cx_.setter 中时,你会一次又一次地调用相同的 cx_.setter,这会导致递归错误