判断实例是否已被销毁
我有一个对象实例列表(wxPython 小部件)。我希望能够判断列表中的实例是否已被销毁。人们会怎样做呢?
I have a list of instances of objects (wxPython widgets). I'd like to be able to tell if an instance within my list has been destroyed. How would one go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
wxPython 小部件在被销毁时为
False
。所以你可以简单地这样做:wxPython widgets are
False
when they're destroyed. So you can simply do this:假设 wxPython 遵守规则,“销毁”意味着“不再被引用”,那么标准库中的
weakref
模块应该可以让你做你想做的事情(具体来说,你可以注册一个创建弱引用时的回调,在弱引用的目标被销毁之前调用)。如果 wxPython 不遵守规则,或者禁用了对其对象的弱引用,那么您可能会不走运。
Assuming wxPython is playing by the rules and by "destroyed" you mean "is no longer referenced", then the
weakref
module in the standard library should let you do what you want (specifically, you can register a callback when creating a weak reference that is invoked just before the target of the weak reference is destroyed).If wxPython isn't playing by the rules, or has disabled weak referencing for its objects, you may be out of luck.
这适用于所有
wx.Window
派生对象。如果它已被销毁或处置,则不会是None
,而是False
。This is applicable to all
wx.Window
derived objects. If it has been destroyed or disposed, it won't beNone
, but it will beFalse
.