isinstance is broken

Michele Simionato mis6 at pitt.edu
Sat Jan 18 11:50:25 EST 2003


There are inconsistencies in "isinstance".

Example 1:

>>> isinstance(int,object)
1

gives no error message. I don't like it, an error should be
raised. There is issubclass for that job:

>>> issubclass(int,object)
1


Example 2:

>>> class C: pass #does *not* inherit from object
>>> c=C() 
>>> isinstance(c,object) #???
1

It is clear that c is *not* an instance of class object: 
for example it doesn't have the __new__ attribute:

>>> c.__new__
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: C instance has no attribute '__new__'

Moreover, on a related note:

>>> import re
>>> reobj=re.compile('x')
>>> isinstance(reobj,object)
1
>>> reobj.__new__
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: __new__

but regular expression objects are *not* instances of object (this is
also documented). I wonder if there is a plan to make regular expression 
objects regular objects in the near future. For the moment, of course I 
can use

class RE(object):
  def __init__(self,regexp):
      reobj=re.compile(regexp) 
      for attr in dir(reobj):
          setattr(self,attr,getattr(reobj,attr))

REobj=RE('x')
REobj.__new__
<built-in method __new__ of type object at 0x80e6280>

Should I interpret the fact that isinstance(something,object) always returns
true as a bug or is it intentional ?

Cheers,

                            Michele




More information about the Python-list mailing list