confused by isinstance()

Francois Petitjean littlejohn.75 at free.fr
Thu Jan 31 03:30:48 EST 2002


After some experiments, I found that :
obj.__class__.__name__  ==  Interpreter.__name__   # 'Interpreter'

I have added a direct call to the onload function :
   interp = Interpreter('stab', 0, 1)  # The ctor calls onload(self) through
a method call of a Module instance
   #  direct call to onload
   print "\n"
   print onload(interp)   # This time, isinstance(obj, Interpreter) is true!

In the first case  `Interpreter`  is <class Interpreter.Interpreter at
00843BDC>
In the second case (direct call) : <class __main__.Interpreter at 007F924C>
In the two cases obj is the same interp object (same address)

Here is what the Interpreter.__init__(fname, quiet, dry_run) does :
a) opens and reads fname+'.mdes' file.The content is decoded :
module interpreteur << eom1
description << eodescr
This module contains the interpreter commands.

For example:
  print a list of all commands
  print how to invoke a specific command
  print the documentation of a Module or a Command
eodescr
mod_name Interpreter
fname_cdes builtin
init_fn onload
eom1
....
[other module description]

So a Module object is built with these informations. (the Module ctor reads
and interprets the builtin.cdes file, it contains a list of Commands)

b) instructs the first Module (named interpreteur) to import the mod_name
python module, to check if an 'init_fn' function exists in this module and
to call it with provided parameters *args, **kwargs.
So, in the called Module method :
mod_name = getattr(self, 'mod_name', self.name)
self.module = __import__(mod_name)
init_fn_name = getattr(self, 'init_fn', None)
if init_fn_name:
    init_fn = getattr(self.module, init_fn_name, None)
    if callable(init_fn):
         mod_objs = (self.module,) + init_fn(*args, **kwargs)  #
Interpreter.onload(..) is called
....

It seems that in "isinstance(obj, Interpreter)" the 'Interpreter'
variable/symbol is bound to two different python objects (addresses are
different). These two objects are two class Interpreter objects in different
namespaces.

Why the result of isinstance(object, cls) depends on the namespace in which
cls lives? What is the proper way to "spell" cls?

Regards
PS Sorry for bad english, it is not my native language. I hope that I was
clear.






More information about the Python-list mailing list