1
imn1 2014-12-20 10:11:57 +08:00
? 没看明白
没有abc的情况下不叫实例化吧? |
2
kcworms 2014-12-20 10:29:37 +08:00 1
用打印语句而不是print函数的时候每打印一个实例就垃圾回收掉一个
http://stackoverflow.com/questions/14011440/python-class-instances-referring-to-same-location |
3
bcxx 2014-12-20 10:38:49 +08:00 1
Interesting, you can find out the reason (GC & print statment vs print function) with dis:
https://gist.github.com/bcho/010a9793776f3b89cdfd |
4
sujin190 2014-12-20 12:31:53 +08:00 1
这个叫对象缓存,如果连续快速实例化然后释放该对象,那么极有可能生成的两个对象的id是一样的
|
5
Jex 2014-12-20 16:04:20 +08:00 2
Python FreeList,看来class new方法也用了这东西:
http://jex.im/programming/python-free-list-glimpse.html 见: http://svn.python.org/projects/python/trunk/Objects/classobject.c ``` PyObject * PyMethod_New(PyObject *func, PyObject *self, PyObject *klass) { register PyMethodObject *im; im = free_list; if (im != NULL) { free_list = (PyMethodObject *)(im->im_self); PyObject_INIT(im, &PyMethod_Type); numfree--; } else { im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); if (im == NULL) return NULL; } im->im_weakreflist = NULL; Py_INCREF(func); im->im_func = func; Py_XINCREF(self); im->im_self = self; Py_XINCREF(klass); im->im_class = klass; _PyObject_GC_TRACK(im); return (PyObject *)im; } ``` |
7
Jex 2014-12-20 16:42:11 +08:00
这个没有用free_list,是直接py malloc管理的
|