Python class method vs. static method

Class methods are methods that have a reference to a class object as their first argument, whereas static methods are methods that may have no parameters.

Static methods do not have access to what the class is. The static method is only a function without access to the object or its internals.

Class methods have access to what the class is. The class method is also a function, but with access to the object and its internals like fields and other methods.

In Python, there is a distinction between class methods and static methods

Before distinguishing between the two methods, class method and static method, let's first create a class with a normal method, a class method, and a static method:

class fresherearth:
    def myfun(self, x):
        print("First parameter (self):", self)
        print("Second parameter (x):", x)

    @classmethod
    def classfun(cls, x):
        print("First parameter (cls):", cls)
        print("Second parameter (x):", x)

    @staticmethod
    def statfun(x):
        print("First parameter (x):", x)

Note: The @classmethod decorator is used to define class methods, whereas the @staticmethod decorator is used to define static methods.

To make an object of the class "fresherearth," say "ob," here is the code:

ob = fresherearth()

Object "ob" has now been created. If we print the "ob" using the statement:

print(ob)

The output looks like this:

<__main__.fresherearth object at 0x00000255CF48DF10>

Here is the code to call a method, say myfun(), using the above-created object, that is, ob. I've passed the string "Hello" as an argument.

ob.myfun("Hello")

The object instance "ob" will be passed as the first argument implicitly. Therefore, "ob" refers to "self." And "Hello" gets passed as a second argument. Therefore, "Hello" refers to "x." Here is the output produced by calling the function myfun() using the above code:

First parameter (self): <__main__.fresherearth object at 0x0000019C1B03DF10>
Second parameter (x): Hello

Let's use the same object "ob" to invoke the class method classfun(). In the case of class methods, the class of the object instance is passed as the first argument implicitly. For example, consider the following statement:

ob.classfun("Hello")

where "ob" is an object of the class named "fresherearth." Therefore, the class "fresherearth" will get implicitly passed as the first argument to classfun(). That is, fresherearth refers to "cls" (the first argument of classfun()), whereas "Hello" refers to "x" (the second argument of classfun()). Here is the output produced by the above code:

First parameter (cls): <class '__main__.fresherearth'>
Second parameter (x): Hello

Also, the class method named classfun() can also be called directly using the class itself. And of course, most programmers define class methods so that they can call those class methods without creating an instance of the class. Here is the code to call the class method named classfun() without an instance of the class:

fresherearth.classfun("Hello")

That is, there is no need to create an object for the class. Just call the class method using the similar code given above. The output should be the same as the previous one, that is:

First parameter (cls): <class '__main__.fresherearth'>
Second parameter (x): Hello

The static method can also be called either directly using the class itself or using the instance of the class. That means it is similar to a class method, but static methods are like normal functions. There is no need for the "self" and "cls" parameters.

Note: The self is an instance of an object, while the cls is the class.

Here is the code to call static methods. Call the static method either using the object instance of the class, that is:

ob.statfun("Hello")

Or by directly calling the method with the help of a class in this way:

fresherearth.statfun("Hello")

The output should be the same in both cases, which is:

First parameter (x): Hello

Python Online Test


« Previous Tutorial Next Tutorial »