MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python Static Methods


We know that a static method in Python is treated differently than in other languages. Static methods in Python are extremely similar to python class methods, but the difference is that a static method is bound to a class rather than the objects for that class.
To define a static method, we use the @staticmethod decorator, which is a built-in decorator. Also, there is no need to import any module to use decorators. Using a static method in a class, we permit it to be accessed only by the class objects or inside the class.

There are few limitations related to static methods, such as:


Unlike, class method, a static method cannot alter or change any variable value or state of the class.
Static methods do not have any knowledge related to the class.


Static methods do not require any knowledge related to the class that they are built-in. They are only formed in a class so that only the class instances can access them. We can use a static method for simple functionality that is mostly not related to the class. For example, we want to add two numbers together, but we do not want our method to be used elsewhere, other than the class or through its instances, so we will create a static method. It will not require any information related to class as it only requires the numbers it has to add, but it will still fulfill its purpose as it is like a personal method that only the class has access to.
Most of the functionalities of the static methods can be performed using a class method. However, we prefer the static method, where it could work to make our program more efficient and faster as we do not have to pass self as a parameter, so the program's efficiency increases.

In Python, static methods can be created using @staticmethod.

class Student:
@staticmethod
    def myfunc():
        //Code to be executed

Alternatively, we can follow the below syntax as well:

staticmethod(class_name.method())


Using @staticmethod annotation is actually a better way to create a static method in Python as the intention of keeping the method static is clear.

Advantages of Python static method


Static methods have a very clear use case. When we need some functionality not for an Object but with the complete class, we make a method static. This is advantageous when we need to create utility methods.
Finally, note that we do not need the self or cls to be passed as the first argument in a static method.

Example



class Employee:
    no_of_leaves = 8

    def __init__(self, aname, asalary, arole):
        self.name = aname
        self.salary = asalary
        self.role = arole

    def printdetails(self):
        return f"The Name is {self.name}. Salary is {self.salary} and role is {self.role}"

    @classmethod
    def change_leaves(cls, newleaves):
        cls.no_of_leaves = newleaves

    @classmethod
    def from_dash(cls, string):
        return cls(*string.split("-"))

    @staticmethod
    def printgood(string):
        print("This is good " + string)

harry = Employee("Harry", 255, "Instructor")
rohan = Employee("Rohan", 455, "Student")
karan = Employee.from_dash("Karan-480-Student")

Employee.printgood("Rohan")


Conclusion

In this page (written and validated by ) you learned about Python Static Methods . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Abstraction and Encapsulation.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.