module.__init__() takes at most 2 arguments error in Python

后端 未结 3 1200
北荒
北荒 2021-01-05 23:16

I have 3 files, factory_imagenet.py, imdb.py and imagenet.py

factory_imagenet.py has:

import datasets.imagenet

It

相关标签:
3条回答
  • 2021-01-06 00:04

    Here's another possible cause...

    If you have an __init__.py file, make sure you import the super class before the derived ones.

    Here's the WRONG way to do it:

    from mymodule.InheritedA import InheritedA
    from mymodule.InheritedB import InheritedB
    from mymodule.Parent import Parent
    

    The above will give an error:

    TypeError: module.__init__() takes at most 2 arguments (3 given)
    

    However this will work:

    from mymodule.Parent import Parent
    from mymodule.InheritedA import InheritedA
    from mymodule.InheritedB import InheritedB
    

    For example the file InheritedA.py might be:

    from mymodule import Parent
    
    class InheritedA(Agent):
        def __init__(self):
            pass
    
        def overridden_method(self):
            print('overridden!!')
    
    0 讨论(0)
  • 2021-01-06 00:06

    When you call datasets.imdb.__init__(self, image_set)
    Your imdb.__init__ method gets 3 arguments. Two you send and third is implicit self

    0 讨论(0)
  • 2021-01-06 00:08
    module.__init__() takes at most 2 arguments (3 given)
    

    This means that you are trying to inherit from a module, not from a class. In fact, datasets.imdb is a module; datasets.imdb.imdb is your class.

    You need to change your code so that it looks like this:

    class imagenet(datasets.imdb.imdb):
        def __init__(self, image_set, devkit_path=None):
            datasets.imdb.imdb.__init__(self, image_set)
    
    0 讨论(0)
提交回复
热议问题