__init__() missing 1 required positional argument

前端 未结 6 1661
死守一世寂寞
死守一世寂寞 2020-12-06 09:35

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 varia

6条回答
  •  鱼传尺愫
    2020-12-06 10:16

    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)
    

提交回复
热议问题