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

4,601 views
Skip to first unread message

Rudi Hammad

unread,
Aug 13, 2018, 1:58:00 PM8/13/18
to Python Programming for Autodesk Maya
Hi,

I was working on an inheritance "tree" when I found that problem.
I have classA that is the base class of classB, and the classB is the base class of both classC1 and classC1.
                         classA
                             |
                        classB
                       /          \
               classC1       classC2

Each class is in a diferent module so, something like

moduleA.py
class ClassA(object):
   
def __init__(self, name):
       
print name

moduleB.py
import moduleA; reload(moduleA)

class ClassB(moduleA.ClassA):
   
def __init__(self,name):
       
super(ClassB, self).__init__(name)

moduleC1.py
import moduleB; reload(moduleB)

class ClassC1(moduleB.ClassB):
   
def __init__(self,name):
       
super(ClassC1, self).__init__(name)

moduleC2.py
Introducir código aquí...import moduleB; reload(moduleB)

class ClassC2(moduleB.ClassB):
   
def __init__(self,name):
       
super(ClassC2, self).__init__(name)


And this is when I get the # Error: TypeError: super(type, obj): obj must be an instance or subtype of type #
import moduleA;
classA
= moduleA.ClassA("foo")
import moduleB;
classB
= moduleB.ClassB("foo")
import moduleC1; reload(moduleC1)
classC1
= moduleC1.ClassC1("foo")  # workd Ok at this point
import moduleC2;
classC2
= moduleC2.ClassC2("foo")

# this time if I do this, I get the error
classC1
= moduleC1.ClassC1("foo")
# Error: TypeError: super(type, obj): obj must be an instance or subtype of type #


I have been reading all day about it. I have read that you should never user super(), then that you always should use super(), then that you should
never reload a module but I need to reload my module while I am developing... So I thought to ask here.

I guess my real question, besides the error which I can read more about it, is what is wrong with the design of that code.In this simple example to create this tree where class C1 and class C2 inherite from the same classB, what am I doing wrong?

Thank you

R


Rudi Hammad

unread,
Aug 13, 2018, 2:00:55 PM8/13/18
to Python Programming for Autodesk Maya
(uppss... sorry about the "Introducir código aquí..." in moduleC2.py example)

Rudi Hammad

unread,
Aug 13, 2018, 3:02:59 PM8/13/18
to Python Programming for Autodesk Maya

By the way, I have printed out the adress of  moduleB.ClassB in moduleC1.py and moduleC2.py.
Because I have added reload(moduleB) in each module, that reload is changing the adress of the class moduleB.ClassB, so when I do the second time classC1 = moduleC1.ClassC1("foo")
there is nothing there because the adress changed. So I get that error. Is that correct?

Still, I am not sure of to design the code to keep that class "tree" mentioned before. I need the reload to update my progress to the subclasses, but by doing so,
the memory address changes.

Justin Israel

unread,
Aug 13, 2018, 4:04:06 PM8/13/18
to python_in...@googlegroups.com
Super is a problem when a class is using multiple inheritance. But you aren't using multiple inheritance here. 

I would recommend to stop using reload() here. When you reload one module it will leave other interdependent modules in conflicting states. As you have identified, the memory address is changing because the reload creates another copy of all the contents in the module. This means the other modules can have old references. The C class may end up having a bad reference if the B class gets redefined. If you really need this reload functionality (which should not be left in place for a production deploy) then you could search for a "deep reload" solution online. This would at least reload everything related to what you are reloading. Or, you could remove the reloads from the modules and just manually reload in the right order as needed:

reload(A) 
reload(B) 
reload(C) 


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/ee76aacf-2e05-471a-9029-56bb3f292194%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rudi Hammad

unread,
Aug 13, 2018, 5:08:51 PM8/13/18
to Python Programming for Autodesk Maya
Cool, thanks for clarifying this. I spent all day thinking what was wrong.
I'll just reload thinks manually during dev.

Cheers

El lunes, 13 de agosto de 2018, 22:04:06 (UTC+2), Justin Israel escribió:
Super is a problem when a class is using multiple inheritance. But you aren't using multiple inheritance here. 

I would recommend to stop using reload() here. When you reload one module it will leave other interdependent modules in conflicting states. As you have identified, the memory address is changing because the reload creates another copy of all the contents in the module. This means the other modules can have old references. The C class may end up having a bad reference if the B class gets redefined. If you really need this reload functionality (which should not be left in place for a production deploy) then you could search for a "deep reload" solution online. This would at least reload everything related to what you are reloading. Or, you could remove the reloads from the modules and just manually reload in the right order as needed:

reload(A) 
reload(B) 
reload(C) 
On Tue, Aug 14, 2018, 7:03 AM Rudi Hammad <rudih...@gmail.com> wrote:

By the way, I have printed out the adress of  moduleB.ClassB in moduleC1.py and moduleC2.py.
Because I have added reload(moduleB) in each module, that reload is changing the adress of the class moduleB.ClassB, so when I do the second time classC1 = moduleC1.ClassC1("foo")
there is nothing there because the adress changed. So I get that error. Is that correct?

Still, I am not sure of to design the code to keep that class "tree" mentioned before. I need the reload to update my progress to the subclasses, but by doing so,
the memory address changes.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages