"Is type a real analogue of isinstance? Or, I asked the other way around, can you imagine the case when isinstance (i, type (i)) is False?"
I do not see a situation in which isinstance(i, type(i)) will not return True .
type() checks and returns the type object from the instance, so there is no reason the return value will complete the isinstance() check.
Digging into the cPython source, we see that the code for type() just returns a type object attached to the instance:
v = (PyObject *)o->ob_type; Py_INCREF(v); return v;
while the first thing that isintance () code does is check if the types match exactly (later it will go into correspondence with the classes in the inheritance chain):
int PyObject_IsInstance(PyObject *inst, PyObject *cls) { _Py_IDENTIFIER(__instancecheck__); PyObject *checker; if (Py_TYPE(inst) == (PyTypeObject *)cls) return 1;
Note that Py_TYPE is just a macro that returns obj->ob_type , which corresponds to the return value of type() . This is defined in Include / object.h as:
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
source share