confused by isinstance()

Francois Petitjean littlejohn.75 at free.fr
Tue Jan 29 22:46:25 CET 2002


Environment: Python 2.1 on Windows NT4 SPx (x== 5 or 6) with IE 4.0  (1)

In a Interpreter.py file :
class Interpreter:
....
# global procedures (module functions)
def onload(obj):
    """Init_fn of Interpret.py"""
    if not getattr(obj, 'quiet', 1):
        print "obj %s type %s\nclass %s isinstance %s" % (`obj`,
type(obj),obj.__class__,isinstance(obj, Interpreter) )
        print "Interpreter %s\nrepr %s\ntype %s" % (Interpreter,
`Interpreter`,type(Interpreter))
        # print "Interpreter %s __class__ %s" % (Interpreter,
Interpreter.__class__)
    # assert isinstance(obj, Interpreter)
    return (obj,)  #  tuple of objects
#  Note that the assert isinstance is commented out

$ ../Python21/python.exe Interpreter.py
obj <__main__.Interpreter instance at 007F7CBC> type <type 'instance'>
class __main__.Interpreter isinstance 0
Interpreter Interpreter.Interpreter
repr <class Interpreter.Interpreter at 00843BDC>
type <type 'class'>

We are in the if __name__ == '__main__': case, obj has an attribute named
'quiet' which is false (in fact 0), it is an instance of Interpreter. The
onload function is called indirectly from the __init__ ctor of Interpreter.
no exception (no TypeError) but isinstance silently returns 0

I can live with the assert commented out, it seems that it is good practice
to avoid use of isinstance or of tests on the types, but I'd rather
understand why in this case isinstance returns false. I suspect some
namespace collision.

(1) Thank you MicroSoft for shipping some system components with an
application ==> It is practically impossible to assert the system's version.

Regards

---
   Ce que l'on conçoit bien s'énonce clairement,
   Et les mots pour le dire arrivent aisément.
        Boileau Despréaux   L'Art Poétique. Chant I





More information about the Python-list mailing list