我在不同的文件中有以下类
class Fruit():
def __init__(self, value=0):
self.value = value
def __add__(self, other):
if type(self) == type(otro):
## PROBLEM AREA ##################################################
return type(self)(self.value + other.value)
else:
raise Exception.SumError(self, other) # Custom exception
def __repr__(self):
return f"{type(self).__name__}({self.value})"
Fruit((类是基类,以下两个子类从中继承
class Pear(Fruit):
"""docs"""
def __init__(self, quantity=0):
super().__init__(quantity)
self.unit = "Pe"
class Apple(Fruit):
"""docs"""
def __init__(self, quantity=0):
super().__init__(quantity)
self.unit = "Ap"
结果中所需的类别如下:
class Market_List():
"""docs"""
def __init__(self, value=0):
self.value = value
目前,我可以用Pears((添加Pears(((,用Apples((添加Apples(((。我的问题是,如何让用Apples添加Pearss((抛出一个Market_List((对象。我已经尝试在Fruit((类的开头使用from market_list import Market_List
,但希望在Market_List((类中也这样做以进行反向操作,然后它进入一个循环并给我一个错误
将inports放入init方法中,那么代码将不会在模块加载时运行,也不会得到错误