【问题标题】:python takes 1 positional argument but 2 were given [duplicate]python接受1个位置参数,但给出了2个[重复]
【发布时间】:2019-04-06 03:37:48
【问题描述】:

我有一个类调用另一个类来使用它的功能

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

我该如何解决这个问题?

【问题讨论】:

  • importImage 缺少 self 参数。

标签: python oop


【解决方案1】:

要么:

class Prepare():

    def importImage(self, image):

或:

class Prepare():

    @staticmethod
    def importImage(image):

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

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 2016-10-13
    • 2020-12-31
    • 2022-01-02
    • 2019-04-20
    • 2018-11-15
    • 2014-07-19
    • 2016-08-09
    相关资源
    最近更新 更多