TypeError: _init_() takes exactly 2 arguments (1 given)

TypeError: _init_() takes exactly 2 arguments (1 given)

我是 python 的新手,想 运行 创建和保存简单的代码 HelloWorld.docx 这是我的 default.py

代码
asd = os.path.join(os.path.abspath("./"), "lib")
jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s" % asd)
hw = HelloWorld()
hw.main()

这是我的 init.py()

代码
class HelloWorld:
def __init__(self, dataDir):
    self.dataDir = dataDir

def main(self):
    """
        : The path to the documents directory. :
    """
    Document = jpype.JClass("com.aspose.words.Document")

    DocumentBuilder = jpype.JClass("com.aspose.words.DocumentBuilder")

    doc = Document()
    builder = DocumentBuilder(doc)

    builder.writeln('Hello World!')
    doc.save(self.dataDir +'HelloWorld.docx')

我收到此错误"TypeError: init() takes exactly 2 arguments (1 given)"我不知道如何解决它,我仔细检查了所有地方,但没有任何帮助

您在这里遗漏了变量 dataDir:

hw = HelloWorld('path/to/your/dir')

根据定义,您的 class Helloworld 的 init 需要 dataDir 的值。

但是在实例化时你没有提供任何东西。

根据上面的答案,你能检查关于缺失 class 的错误是否与这个 link 相同吗?

JPype class not found

当我们创建class的对象时,会调用__init__函数。 __init__ 函数接受两个参数,一个是 class 实例 self,另一个是 dataDir

__init__ 将初始化 class 或对象的实例。

__init__ 函数称为构造函数或初始化程序,当您创建 class.[= 的新实例时会自动调用该函数19=]