社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Jerry Halisberry • 3 年前 • 1062 次点击  

我想做点像

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
 
1062 次点击  
文章 [ 1 ]  |  最新文章 3 年前