Python __init__() 缺少 1 个必需的位置参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19929626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
__init__() missing 1 required positional argument
提问by eddard.stark
I am trying to learn Python. This is a really simple code. All I am trying to do here is to call a class's constructor. Initialize some variables there and print that variable. But it is giving me an error. It is saying:
我正在尝试学习 Python。这是一个非常简单的代码。我在这里要做的就是调用类的构造函数。在那里初始化一些变量并打印该变量。但它给了我一个错误。它在说:
missing 1 required positional argument
缺少 1 个必需的位置参数
Here is my code:
这是我的代码:
class DHT:
def __init__(self, data):
self.data['one'] = '1'
self.data['two'] = '2'
self.data['three'] = '3'
def showData(self):
print(self.data)
if __name__ == '__main__': DHT().showData()
采纳答案by Mike
You're receiving this error because you did not pass a data
variable to the DHT constructor.
您收到此错误是因为您没有将data
变量传递给 DHT 构造函数。
aIKid and Alexander's answers are nice but it wont work because you still have to initialize self.data
in the class constructor like this:
aIKid 和 Alexander 的答案很好,但它不起作用,因为您仍然必须self.data
在类构造函数中进行初始化,如下所示:
class DHT:
def __init__(self, data=None):
if data is None:
data = {}
else:
self.data = data
self.data['one'] = '1'
self.data['two'] = '2'
self.data['three'] = '3'
def showData(self):
print(self.data)
And then calling the method showData like this:
然后像这样调用 showData 方法:
DHT().showData()
Or like this:
或者像这样:
DHT({'six':6,'seven':'7'}).showData()
or like this:
或者像这样:
# Build the class first
dht = DHT({'six':6,'seven':'7'})
# The call whatever method you want (In our case only 1 method available)
dht.showData()
回答by viraptor
Your constructor is expecting one parameter (data). You're not passing it in the call. I guess you wanted to initialise a field in the object. That would look like this:
您的构造函数需要一个参数(数据)。你没有在通话中传递它。我猜你想初始化对象中的一个字段。那看起来像这样:
class DHT:
def __init__(self):
self.data = {}
self.data['one'] = '1'
self.data['two'] = '2'
self.data['three'] = '3'
def showData(self):
print(self.data)
if __name__ == '__main__':
DHT().showData()
Or even just:
或者甚至只是:
class DHT:
def __init__(self):
self.data = {'one': '1', 'two': '2', 'three': '3'}
def showData(self):
print(self.data)
回答by aIKid
You need to pass some data into it. An empty dictionary, for example.
您需要向其中传递一些数据。例如,空字典。
if __name__ == '__main__': DHT('a').showData()
However, in your example a parameter is not even needed. You can declare it by just:
但是,在您的示例中,甚至不需要参数。您可以通过以下方式声明它:
def __init__(self):
Maybe you mean to set it from the data?
也许您的意思是从数据中设置它?
class DHT:
def __init__(self, data):
self.data['one'] = data['one']
self.data['two'] = data['two']
self.data['three'] = data['three']
def showData(self):
print(self.data)
if __name__ == '__main__': DHT({'one':2, 'two':4, 'three':5}).showData()
showData
will print the data you just entered.
showData
将打印您刚刚输入的数据。
回答by Alexander Zhukov
You should possibly make data
a keyword parameter with a default value of empty dictionary:
您可能应该data
使用默认值为空字典的关键字参数:
class DHT:
def __init__(self, data=dict()):
self.data['one'] = '1'
self.data['two'] = '2'
self.data['three'] = '3'
def showData(self):
print(self.data)
if __name__ == '__main__':
DHT().showData()
回答by Eyasu
The problem is with, you
问题是,你
def __init__(self, data):
when you create object from DHT class you should pass parameter the data should be dict type, like
当你从 DHT 类创建对象时,你应该传递参数,数据应该是 dict 类型,比如
data={'one':1,'two':2,'three':3}
dhtObj=DHT(data)
But in your code youshould to change is
但是在你的代码中你应该改变的是
data={'one':1,'two':2,'three':3}
if __name__ == '__main__': DHT(data).showData()
Or
或者
if __name__ == '__main__': DHT({'one':1,'two':2,'three':3}).showData()