我想做点像
class my_class:
def __add__(self, other: "my_class") -> "my_class":
if isinstance(other, some_other_class):
return other + self
elif isinstance(other, my_class):
return <special_adding_technique>
else:
raise NotImplementedError()
但我不能,因为我不能引用我的_类本身。我可以用try-except或check来打字
has_attr
,但这远不如简单地检查干净
isinstance
.我能做些什么,既简单又不显得粗糙?