简体   繁体   English

Python3-TypeException在给定的情况下采用1个参数2

[英]Python3 - TypeException take 1 argument 2 where given [closed]

The program i am working on have a class with constructor defined as follow : 我正在研究的程序有一个类,其构造函数定义如下:

def Oracle(object) :
Agold = None
sentence = None

def __init__(self, sentence, Agold):
    self.Agold = Agold
    self.sentence = sentence

but, when i call the constructor in my main method, as follow : 但是,当我在main方法中调用构造函数时,如下所示:

oracle = Oracle(words, ref_tree)

python 3 give me this error : python 3给我这个错误:

Traceback (most recent call last):
File "oracle_test.py", line 52, in test_exemple
oracle = Oracle(words, ref_tree)
TypeError: Oracle() takes 1 positional argument but 2 were given

i don't understand the origin of this problem, and i don't see what gone wrong. 我不了解此问题的根源,也看不出出了什么问题。

Can someone give me an explanation ? 有人可以给我一个解释吗? Thanks 谢谢

You defined Oracle as a function instead of a class. 您将Oracle定义为函数而不是类。 Use class instead of def . 使用class而不是def Also, assuming Agold and sentence are supposed to be instance variables instead of class variables, Agold = None and sentence = None are not needed (see this ). 此外,假设Agoldsentence都应该是实例变量,而不是类变量, Agold = Nonesentence = None不需要(见 )。

class Oracle(object):
    def __init__(self, sentence, Agold):
        self.Agold = Agold
        self.sentence = sentence

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM