python类 del,Python上的类del

Lets assume we have a class in python:

class A(object):

def __del__(self):

print "Del!"

__del__ is called upon deleting/garbage collection of any A instance.

Is is possible to do the same for a class? I would like to have some method called when the class itself is garbage collected, which I assume is being done at the script exit.

Thanks in advance for any pointers!

Edit: Just as I have expected, everyone is trying to drive me away from using this technique (I would probably make such a comment myself:)), though the question still stands: is it possible?

I want to the the following: I have a class with a static member that needs to be cleaned.

class A(object):

class Meta(type):

def __new__(mcs, name, bases, attrs):

attrs['conn'] = sqlite3.connect( DB_FILE )

return type.__new__(mcs, name, bases, attrs)

__metaclass__ = Meta

I would like A.conn.close() to be called, but just before the program closes - i.e. when I know that no more instances of A will be ever created. I know I can do this with atexit, but this just seems very ugly.

解决方案

The problem is that classes contain circular references back to themselves - so when they are deleted they are not easily collected - thus the __del__ method odf the metaclass is not called.

I could trigger it being called using Pypy's Python implementation, but not using cpython - either 2.6 or 3.2. And even to trigger that, I had to manually invoke the garbage collector -

The Python environment at program exit is known to be full of inconsitencies, and the chances of the __del__ method being called while enough internal information on the class would exist to allow a sae shut down would be very slim.

Here is my Pypy session where I did trigger the call to the class' __del__

~]$ pypy

Python 2.5.2 (78826, Nov 29 2010, 00:18:05)

[PyPy 1.4.0] on linux2

Type "help", "copyright", "credits" or "license" for more information.

And now for something completely different: ``sorry, I'll teach the cactus how

to swim later''

>>>> import gc

>>>> class Meta(type):

.... def __del__(cls):

.... print ("Arghh!!")

....

>>>> class A(object):

.... __metaclass__ = Meta

....

>>>> del A

>>>> gc.collect()

Arghh!!

0

>>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值