删除实例时删除属性

发布于 2024-08-19 13:49:21 字数 354 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稀香 2024-08-26 13:49:21

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 the A 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.

原来是傀儡 2024-08-26 13:49:21

要控制类 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) in A. 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.

生寂 2024-08-26 13:49:21

一个丑陋的方法是:

def __del__(self):
    for x in dir(self.__class__):
        if type(getattr(self.__class__, x)) == property:
            getattr(self.__class__, x).fdel(self)

An ugly way to do it would be :

def __del__(self):
    for x in dir(self.__class__):
        if type(getattr(self.__class__, x)) == property:
            getattr(self.__class__, x).fdel(self)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文