Search code examples
pythoninheritanceimporttypeerror

"TypeError: module() takes at most 2 arguments (3 given)" when trying to inherit from a base class with the same name as its module


I have the following project structure:

- equalizer
    - __init__.py
    - equalizer.py
    - lms.py
- main.py

__init__.py is empty.

equalizer.py defines a base class named equalizer, like so:

class equalizer:
    var1 = []
    def __init__(self, data):
        self.var1 = data

lms.py defines a child class lms, which derives from equalizer:

from equalizer import equalizer

class lms(equalizer):
    def __init__(self, data):
        super().__init__(self, data)

    def lms(self):
        print('Hello World!')

Finally, in main.py, I instantiate the child:

import equalizer.lms as eq

if __name__ == "__main__":
    data = []

    lms_eq = eq.lms(data= data)
    lms_eq.lms()

When I run the code starting from main.py, I get this error:

Traceback (most recent call last):
  File "C:\Users\...\main.py", line 4, in <module>
    import equalizer.lms as eq
  File "C:\Users\...\equalizer\lms.py", line 6, in <module>
    class lms(equalizer):
TypeError: module() takes at most 2 arguments (3 given)

What is wrong, and how do I fix it?


Solution

  • Make your life easier and have python class names as camel caps to distinguish them from the module.

    The issue is right here:

    from equalizer import equalizer
    

    You didn't relative import the module, so this is more like:

    from <package equalizer> import <equalizer.py>
    

    You want:

    from .equalizer import equalizer
    

    Which makes it more akin to:

    from <equalizer.py> import <class equalizer>