加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如果为true:destruct Class

发布时间:2020-12-20 11:35:46 所属栏目:Python 来源:网络整理
导读:我想知道 Python是否有办法避免__init__中的其他函数并直接进入__del__.对于例如 class API: Array = {"status" : False,"result" : "Unidentified API Error"} def __init__(self,URL): self.isBanned() print "This should be ignored." def isBanned(self
我想知道 Python是否有办法避免__init__中的其他函数并直接进入__del__.对于例如

class API:

    Array = {"status" : False,"result" : "Unidentified API Error"}

    def __init__(self,URL):

        self.isBanned()
        print "This should be ignored."

    def isBanned(self):

        if True:
            goTo__del__()

    def __del__(self):
        print "Destructed"

API = API("http://google.com/");

解决方法

是.这就是例外情况.

class BannedSite(Exception):
    pass

class API:

    Array = {"status" : False,URL):    
        if self.isBanned(URL):
            raise BannedSite("Site '%s' is banned" % URL)
        print "This should be ignored."

    def isBanned(self,URL):
        return True

在__init__方法中引发异常,因此永远不会完成赋值,因此实例没有引用,会立即删除.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读