考虑以下的Python的ctypes - C++结合:的Python ctypes的,C++对象的破坏
// C++
class A
{
public:
void someFunc();
};
A* A_new() { return new A(); }
void A_someFunc(A* obj) { obj->someFunc(); }
void A_destruct(A* obj) { delete obj; }
# python
from ctypes import cdll
libA = cdll.LoadLibrary(some_path)
class A:
def __init__(self):
self.obj = libA.A_new()
def some_func(self):
libA.A_someFunc(self.obj)
什么是删除C++对象的最好方式,当不需要Python对象了。
[编辑] 我说有人建议删除功能,但问题仍然存在由谁来当函数被调用。它应该尽可能方便。
何时调用__del__函数? – tauran 2010-09-16 08:42:58
当“A”的引用计数达到零时,就在清理对象之前。 – 2010-09-16 08:44:43
这种情况何时发生?在python文档中找不到它们。这是否意味着python决定何时执行__del__? – tauran 2010-09-16 08:47:56