Py学习  »  Python

Python是类在同一个类的定义中的实例

Jerry Halisberry • 3 年前 • 1200 次点击  

我想做点像

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 .我能做些什么,既简单又不显得粗糙?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133576
 
1200 次点击  
文章 [ 1 ]  |  最新文章 3 年前