class method with no arguments produces TypeError

后端 未结 3 1274
后悔当初
后悔当初 2021-01-01 02:40

This code:

class testclass:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.test()

    def test():
        print(\'test\')

i         


        
3条回答
  •  忘掉有多难
    2021-01-01 02:50

    Pass self to your test method:

    def test(self):
        print('test')
    

    You need to do this because Python explicitly passes a parameter referring to the instantiated object as the first parameter. It shouldn't be omitted, even if there are no arguments to the method (because of the error specified).

提交回复
热议问题