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

A

Art

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.

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? It doesn't occur if I using for the first
time in a fresh interpreter session.

---> 32 class BiasComponent(ModelComponent):
33 def __init__(self, g, model):
34 super(BiasComponent, self).__init__(g, model)

TypeError: super(type, obj): obj must be an instance or subtype of
type

Seems like the self passed to __init__ is messed up in some way.

Thanks!
 
C

Chris Rebert

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.

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? It doesn't occur if I using for the first
time in a fresh interpreter session.

---> 32 class BiasComponent(ModelComponent):
     33     def __init__(self, g, model):
     34         super(BiasComponent, self).__init__(g, model)

TypeError: super(type, obj): obj must be an instance or subtype of
type

Seems like the self passed to __init__ is messed up in some way.

I would guess you're running into one of the caveats of using
reload(); see http://docs.python.org/library/functions.html#reload

Printing out id(BiasComponent) and id(type(obj)) both before and after
the reload() call should be instructive.

Cheers,
Chris
 
T

Terry Reedy

Art said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top