class method with no arguments produces TypeError

后端 未结 3 1242
后悔当初
后悔当初 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:58

    You call the methods as self.test(). You should mentally translate that to test(self) to find out how the call will be "received" in the function's definition. Your definition of test however is simply def test(), which has no place for the self to go, so you get the error you observed.

    Why is this the case? Because Python can only look up attributes when specifically given an object to look in (and looking up attributes includes method calls). So in order for the method to do anything that depends on which object it was invoked on, it needs to receive that object somehow. The mechanism for receiving it is for it to be the first argument.

    It is possible to tell Python that test doesn't actually need self at all, using the staticmethod decorator. In that case Python knows the method doesn't need self, so it doesn't try to add it in as the first argument. So either of the following definitions for test will fix your problem:

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

    OR:

    @staticmethod
    def test():
        print('test')
    

    Note that this is only to do with methods invoked on objects (which always looks like some_object.some_method(...)). Normal function invocation (looking like function(...)) has nothing "left of the dot", so there is no self, so it won't be automatically passed.

提交回复
热议问题