文档说:
每个类都保留一个对其直接子类的弱引用列表。此方法返回所有仍然存活的引用的列表。
但是,每个类如何首先获得其子类的弱引用列表?换句话说,当我创建
class B(A):
pass
A
如何发现B
只是将其子类化了?并且这种机制是否足够强大以存在边缘情况(自定义元类,赋值给__bases__
等)?
答案 0 :(得分:6)
作为新类初始化的一部分,对该类的弱引用将添加到其每个基类的tp_subclasses
成员中。您可以在Objects/typeobject.c
中的Python源代码中看到这一点:
int
PyType_Ready(PyTypeObject *type)
{
...
/* Link into each base class's list of subclasses */
bases = type->tp_bases;
n = PyTuple_GET_SIZE(bases);
for (i = 0; i < n; i++) {
PyObject *b = PyTuple_GET_ITEM(bases, i);
if (PyType_Check(b) &&
add_subclass((PyTypeObject *)b, type) < 0)
goto error;
}
...
}
static int
add_subclass(PyTypeObject *base, PyTypeObject *type)
{
int result = -1;
PyObject *dict, *key, *newobj;
dict = base->tp_subclasses;
if (dict == NULL) {
base->tp_subclasses = dict = PyDict_New();
if (dict == NULL)
return -1;
}
assert(PyDict_CheckExact(dict));
key = PyLong_FromVoidPtr((void *) type);
if (key == NULL)
return -1;
newobj = PyWeakref_NewRef((PyObject *)type, NULL);
if (newobj != NULL) {
result = PyDict_SetItem(dict, key, newobj);
Py_DECREF(newobj);
}
Py_DECREF(key);
return result;
}
__bases__
的{{3}}也会更新旧基础和新基础的子类列表:
static int
type_set_bases(PyTypeObject *type, PyObject *new_bases, void *context)
{
...
if (type->tp_bases == new_bases) {
/* any base that was in __bases__ but now isn't, we
need to remove |type| from its tp_subclasses.
conversely, any class now in __bases__ that wasn't
needs to have |type| added to its subclasses. */
/* for now, sod that: just remove from all old_bases,
add to all new_bases */
remove_all_subclasses(type, old_bases);
res = add_all_subclasses(type, new_bases);
update_all_slots(type);
}
...
}
请注意,如果元类执行某些操作来自定义子类关系的含义,__subclasses__
将不会反映这一点。例如,issubclass(list, collections.abc.Iterable)
为True
,但从list
开始搜索__subclasses__
树时,collections.abc.Iterable
不会显示。