代码之家  ›  专栏  ›  技术社区  ›  Cristian

python接受1个位置参数,但给出了2个[duplicate]

  •  -2
  • Cristian  · 技术社区  · 6 年前

    我有一个类调用另一个类来使用它的函数

    main.py
    --------------------
    class MyClass():
    
        def main(self, arg):
            from lib.otherclass import OtherClass
            otherClass = OtherClass() 
    
            result = otherClass.prepare.importImage(image)
    
    
    myClass = MyClass()
    final = myClass(image)
    

    我得到这个错误

    importImage() takes 1 positional argument but 2 were given
    

    这是另一个类的外观:

    class OtherClass():
        def __init__(self):
            self.prepare = Prepare()
    
    class Prepare():
    
        def importImage(image):
            blah blah blah
    

    我该怎么解决这个问题?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Sebastian Loehner    6 年前

    class Prepare():
    
        def importImage(self, image):
    

    或:

    class Prepare():
    
        @staticmethod
        def importImage(image):
    

    python takes 1 positional argument but 2 were given What is the purpose of self?