Is types.InstanceType no longer valid with Python 2.2
Roeland Rengelink
r.b.rigilink at chello.nl
Fri Jan 4 18:24:10 EST 2002
Skip Montanaro wrote:
>
[snip]
>
> 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
> bits together in a simple example, starting from a fresh interpreter prompt:
>
> >>> class foo: pass
> ...
> >>> f1 = foo()
> >>> f1.__class__
> <class __main__.foo at 0x821b964>
> >>> f1.__class__.__bases__
> ()
> >>> isinstance(f1, foo)
> 1
> >>> isinstance(f1, object)
> 1
> >>> issubclass(foo, object)
> 0
>
> I thought I understood this stuff, but my brain is about to explode, so I'll
> have to stop here.
>
That surprised me a lot too. However, I noticed that:
>>> class foo:pass
...
>>> isinstance(foo(), object)
1
>>> issubclass(foo, object)
0
>>> issubclass(type(foo()), object)
1
>>> import types
>>> issubclass(types.InstanceType, object)
1
>>> types.InstanceType.__bases__
(<type 'object'>,)
It may be that foo() appears to be an instance of object because
type(foo()) is a subclass of object. not because object is some sort of
hidden baseclass of foo. The surprise then, comes from type(foo()) !=
foo for old style classes.
Since one of the results of healing the type class dichotomy would be
type(foo()) == foo, and hence
isinstance(A(), B) == issubclass(type(A()), B),
I have halfway convinced myself that it does make sense after all.
Hope this helps,
Roeland
--
r.b.rigilink at chello.nl
"Half of what I say is nonsense. Unfortunately I don't know which half"
More information about the Python-list
mailing list