codescracker
python

Python class method Vs static method



« Python Tutorial Python Examples »

Class methods are methods that have reference to a class object as 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 and 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.

Difference between ClassMethod and StaticMethod in Python

Before differentiating these two methods, that is, class method and static method. Let's first create a class having a normal method, a class method, and a static method:

class CodesCracker:
    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, used to define class methods, whereas @staticmethod decorator, used to define static methods.

Now to create an object say ob of the class CodesCracker. Here is the code:

ob = CodesCracker()

Now object ob is created. If we print the ob using the statement:

print(ob)

The output looks like:

<__main__.CodesCracker object at 0x00000255CF48DF10>

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

ob.myfun("Hello")

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

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

Now let's use the same object ob to call the class method named classfun(). In case of class methods, the class of object instance will get implicitly passed as first argument. For example, consider the following statement:

ob.classfun("Hello")

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

First parameter (cls): <class '__main__.CodesCracker'>
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:

CodesCracker.classfun("Hello")

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

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

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

Note - The self refers to object instance, whereas the cls refers to the class.

Here is the code to call static methods. Call 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 class in this way:

CodesCracker.statfun("Hello")

In both the cases, the output would be same, that will be:

First parameter (x): Hello

Python Online Test


« Python Tutorial Python Examples »



© Copyright 2021. All Rights Reserved.

CodesCracker