isinstance(obj, type(obj)) == True?

Terry Reedy tjreedy at udel.edu
Wed Jun 24 14:31:44 EDT 2009


Art wrote:
> I have the following problem:
> 
> ipdb> p type(self)
> <class 'component.BiasComponent'>
> 
> ipdb> isinstance(self, component.BiasComponent)
> False
> 
> I thought that isinstance(obj, type(obj)) == True.

Yes, but that is not what you entered ;-).
The name 'component.BiasComponent' is not bound to type(self),
but to another object, probably with the same .__name__ attribute.

> The specific problem is when I try to call the super of a class and it
> only occurs after 'reload'ing the file in the interpreter. What am I
> messing up by reloading?

The relationship between namespace names and definition names. My guess 
is that you have two class objects with the same definition name. This 
happens when you reload the code for a class and have instances that 
keep the original class object alive. This sort of problem is why reload 
was *removed* from Py3. It did not work the way people wanted it to and 
expected it to. I recommend not to use it.

 > It doesn't occur if I using for the first
> time in a fresh interpreter session.

Starting fresh is the right thing to do. IDLE has a 'Restart Shell' 
command for this reason. It automatically restarts when you run a file. 
You have already wasted more time with reload than most people would 
ever save in a lifetime of using reload, even if it worked as expected.

Terry Jan Reedy




More information about the Python-list mailing list