0

Ex 1:

class foo:
    def test():
        print("test completed")
        
obj = foo()
obj.test()

RESULT: TypeError: test() takes 0 positional arguments but 1 was given

Ex 2:

class foo:
    def test():
        print("test completed")
        
obj = foo
obj.test()

OUTPUT:- test completed

EX 3:

class foo:
    def test(self):
        print("test completed")
        

obj = foo()
obj.test()

OUTPUT: test completed

  • If your function does not depend on any particular instance's state, then it does not require `self` and therefore should be `@classmethod` or `@staticmethod` as appropriate – Cory Kramer Oct 23 '20 at 14:17
  • 1
    First case: you made an instance and called the method through the instance. This calls implicitly sending a ```self``` to the method as a result you are given an error warning. Second case: Well, you are assigned a variable ```class``` to another variable and then call through it. Because the method doesn't need a `self`` argument then it simply becomes a ```simple function``` Third case: Same as first case, but you explicitly stated an ```self``` inside the method, so it, well, works. – Henry Tjhia Oct 23 '20 at 14:17

0 Answers0