python - 如果不满足条件,如何从对象返回和删除
问题描述
如果满足特定条件,我想要一个不创建对象的类
class MyClass:
def __init__(self, input):
if input == condition:
# get out of here and destroy this object to save memory
self.var1 = input;
# etc...
满足条件后,对象被销毁。
解决方案
我建议提出一个例外
class BadInitArgs(Exception):
pass
class MyClass:
def __init__(self, input):
if input != "expected":
raise BadInitArgs
# get out of here and destroy this object to save memor
# etc...
a = MyClass("expected")
b = MyClass("unexpected")
推荐阅读
- istio - ISTIO 1.9.5 - 如何在 istio 发生本地配置更改时发送通知
- javascript - 导入 sweetalert:“未捕获的 SyntaxError:无法在模块外使用 import 语句”
- ios - 如何在 Swift 中显示 IPFS 图像
- unity3d - 在 Admob 中使用广告中介时无法检索 Unity 广告的数据
- gnupg - gpg 2.2.12 在导入密钥失败时的退出代码与 2.2.4 不同
- node.js - 缺少 Cloudfront 提供的错误密钥对 ID
- php - 如何将链接转换为 seo 友好的链接?
- javascript - 如果其他情况下,如何使用 html/javascript 复选框函数链接 python
- php - ajax 函数 .done 和 .fail,无值返回
- python - 将 SQL 表命名为与 python to_sql() 函数中的 df 名称相同