Is types.InstanceType no longer valid with Python 2.2
Alex Martelli
aleax at aleax.it
Fri Jan 4 14:29:28 EST 2002
Skip Montanaro wrote:
...
> How can foo be a classic class with no bases, f1 be both an instance of
> foo
> and of object, but foo not be a subclass of object? Just to pull all the
Implementatively speaking, it boils down to half a line in source file:
Objects/typeobject.c:
387 int
388 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
389 {
...
392 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
393 return b == a || b == &PyBaseObject_Type;
The "half a line" being the part of line 393 from the || to the ; --
basically, this says "if the second argument is builtin `object', then,
yes, type a IS a subtype of it, WHATEVER type a may be".
The insinstance built-in boils down to PyType_IsSubtype when the second
argument is not a class -- in Objects/abstract.c:
1890 int
1891 PyObject_IsInstance(PyObject *inst, PyObject *cls)
1892 {
...
1897 if (PyClass_Check(cls) && PyInstance_Check(inst)) {
...
1902 else if (PyType_Check(cls)) {
1903 retval = PyObject_TypeCheck(inst, (PyTypeObject
*)cls);
OTOH, PyObject_IsSubclass (lines 1946 ff of the same source file) does
not call PyType_IsSubtype under any circumstances -- rather, it works
via abstract_get_bases (lines 1842 ff, same source).
I'm not implying this is RIGHT, mind you -- I have no opinion on that yet;
it's just what IS happening in this case you're observing.
Alex
More information about the Python-list
mailing list