By angelo086

2019-02-11 09:11:19 8 Comments

I am trying to figure out how to have a child class reside in another module. Currently it is more convenient for me to store the parent and child classes in different modules due to their size. I need the super method, since I want to inherit not just all the functions, but the variables in self as well. My current solution is as follows:

Parent Module (

class A:

    def __init__(self, *args, **kwargs):
        super(A, self).__init__(*args, **kwargs)

Child Module(

from parent import A

class B(A):

    def __init__(self, *args, **kwargs):
        super(B, self).__init__(*args, **kwargs)


When I run the child module I get the following error.

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

I understand that this is due to the module reloading and thus causing data to be lost, however I am not sure if there is a workaround.


@filmor 2019-02-11 09:26:45

First, on your code:

  • It's not necessary to always call the parent constructor, in particular calling object's constructor as you do in parent.A is not needed
  • In Python 3, you can use the much simpler super().__init__ form for the call for single-inheritance
  • The import should usually be relative: from .parent import A

Now, to your actual problem:

When you reload parent in this case, you essentially generate a new class object for A that is not identical to the one that your compiled B knows of. You can check this by comparing id(B.__base__) to id(A) after the reload. This is not a problem for the super() form, as that doesn't use the name A explicitly (which points to the new class) but instead uses the actual base class. So it will construct fine, but with the "old" A implementation.

P.S.: It is essential that your question includes information on what you are actually trying to do, in this case reloading a module, which is not a "standard" operation in Python (that's why it's so cumbersome to do).

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Calling a function of a module by using its name (a string)

  • 2008-08-06 03:36:08
  • ricree
  • 598922 View
  • 1480 Score
  • 11 Answer
  • Tags:   python object

15 Answered Questions

[SOLVED] Call a parent class's method from child class in Python?

17 Answered Questions

[SOLVED] Importing modules from parent folder

3 Answered Questions

[SOLVED] python decorator for class methods

  • 2018-02-05 06:25:12
  • Mr.Dreamer
  • 1142 View
  • 1 Score
  • 3 Answer
  • Tags:   python decorator

2 Answered Questions

[SOLVED] Override __mul__ from a child class using parent implementation: leads to problems

  • 2010-11-20 16:03:48
  • Fabio Varesano
  • 3131 View
  • 1 Score
  • 2 Answer
  • Tags:   python inheritance

5 Answered Questions

[SOLVED] How to enforce method signature for child classes?

1 Answered Questions

[SOLVED] referencing class members from child class

2 Answered Questions

[SOLVED] Can't add field to ModelForm at __init__

3 Answered Questions

[SOLVED] Python Child cannot use a Module the Parent Imported

Sponsored Content