Is types.InstanceType no longer valid with Python 2.2

Skip Montanaro skip at pobox.com
Fri Jan 4 12:19:25 EST 2002


    >>> f = foo()
    >>> type(f)
    <type 'instance'>
    >>> type(f) == types.InstanceType
    1
    >>> f2 = foo2()
    >>> type(f2)
    <class '__main__.foo2'>
    >>> type(f2) == types.InstanceType
    0

Hmmm...  Now I'm a bit perplexed.  There's a whole lot of meta-stuff going
on I think.  First, I tried this:

    >>> hasattr(f1, "__class__")
    1
    >>> hasattr(f2, "__class__")
    1

That seemed okay, except all the type constructors also have __class__
attributes:

    >>> hasattr(list, "__class__")
    1
    >>> hasattr(object, "__class__")
    1
    >>> hasattr(int, "__class__")
    1

Bummer.  Next I tried:

    >>> type(f1) == types.InstanceType or isinstance(f1, object)
    1
    >>> type(f2) == types.InstanceType or isinstance(f2, object)
    1

Seemed logical, except the type constructors are also instances of object:

    >>> type(list) == types.InstanceType or isinstance(list, object)
    1

The last thing I tried was using issubclass, but this gave me some bizarre
results:

    >>> isinstance(f1, object)
    1

Huh?  I thought, "so foo must be a subclass of object", but it's not:

    >>> issubclass(foo, object)
    0

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.

Skip




More information about the Python-list mailing list