我想知道是否有办法让 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, "result" : "Unidentified API Error"}
def __init__(self, URL):
if self.isBanned(URL):
raise BannedSite("Site '%s' is banned" % URL)
print "This should be ignored."
def isBanned(self, URL):
return True
__init__
方法中引发了异常,因此赋值从未完成,因此该实例没有引用并立即被删除。
关于python - 如果为真 : destruct Class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23983614/