删除实例时删除属性
A类: def __get(自我): 返回 self._x
def __set(self, y):
self._x = y
def __delete_x(self):
print('DELETING')
del self._x
x = property(__get,__set,__delete_x)
b = A()
# Here, when b is deleted, i'd like b.x to be deleted, i.e __delete_x()
# called (and for immediate consequence, "DELETING" printed)
del b
class A:
def __get(self):
return self._x
def __set(self, y):
self._x = y
def __delete_x(self):
print('DELETING')
del self._x
x = property(__get,__set,__delete_x)
b = A()
# Here, when b is deleted, i'd like b.x to be deleted, i.e __delete_x()
# called (and for immediate consequence, "DELETING" printed)
del b
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
del
语句的语义并不能真正满足您的需求。del b
simple 从本地作用域框架/字典中删除对刚刚实例化的A
对象的引用;这不会直接导致对对象本身执行任何操作。如果这是对该对象的最后一次引用,则引用计数降至零,或者垃圾收集器收集一个周期,可能会导致该对象被释放。您可以通过向对象添加 __del__ 方法或添加不过,后两种解决方案似乎都不是一个好主意。
__del__
方法阻止垃圾收集器收集涉及该对象的任何循环;虽然弱引用不会遇到这个问题,但在任何一种情况下,您都可能在一个奇怪的环境中运行(例如在程序关闭期间),这可能会使您难以完成您想要完成的任务。如果您可以扩展您的确切用例,则可能有一种完全不同的方法来实现您期望的最终目标,但很难根据这样一个普遍且有限的示例进行推测。
The semantics of the
del
statement don't really lend themselves to what you want here.del b
simple removes the reference to theA
object you just instantiated from the local scope frame / dictionary; this does not directly cause any operation to be performed on the object itself. If that was the last reference to the object, then the reference count dropping to zero, or the garbage collector collecting a cycle, may cause the object to be deallocated. You could observe this by adding a__del__
method to the object, or by adding a weakref callback that performs the desired actions.Neither of the latter two solutions seems like a great idea, though;
__del__
methods prevent the garbage collector from collecting any cycles involving the object; and while weakrefs do not suffer from this problem, in either case you may be running in a strange environment (such as during program shutdown), which may make it difficult to get done what you want to accomplish.If you can expand on your exact use case, it may be that there is an entirely different approach to accomplishing your desired end goal, but it is difficult to speculate based on such a general and limited example.
要控制类
A
的实例消失时发生的情况(无论是被删除还是垃圾回收),您可以在A 中实现特殊方法
。如果您希望在该实例的特定属性消失时涉及您的代码,您可以使用具有__del__(self)
__del__
的包装类来包装该属性,或者可能更好在大多数情况下,使用 weakref 模块(但是,并非所有类型都会成为弱引用的目标,因此您可能还需要对这种情况进行一些包装)。如果可以的话,通常最好避免
__del__
,因为它会干扰垃圾回收,从而在存在循环引用时导致“内存泄漏”。To control what happens when an instance of class
A
goes away (whether by being deleted or garbage collected), you can implement special method__del__(self)
inA
. If you want to have your code involved when a specific attribute of that instance goes away, you can either wrap that attribute with a wrapper class which has__del__
, or, probably better in most cases, use the weakref module (however, not all types are subject to being target of weak references, so you may also need some wrapping for this case).Avoiding
__del__
is generally preferable, if you possibly can, because it can interfere with garbage collection and thereby cause "memory leaks" if and when you have circular references.一个丑陋的方法是:
An ugly way to do it would be :