Why always add self as first argument to class methods? [duplicate] Why always add self as first argument to class methods? [duplicate] python python

Why always add self as first argument to class methods? [duplicate]


Because explicit is better than implicit. By making the parameter an explicit requirement, you simplify code understanding, introspection, and manipulation. It's further expanded on in the Python FAQ.

Moreover, you can define class methods (take a class instead of an instance as the first argument), and you can define static methods (do not take a 'first' argument at all):

class Foo(object):    def aninstancemethod(self):        pass    @classmethod    def aclassmethod(cls):        pass    @staticmethod    def astaticmethod():        pass


Guido explained that here. Basically, methods are functions, and functions should not accept any "hidden" parameters, otherwise higher-order facilities, like decorators, won't be able to deal with them in a sensible way.