Python staticmethod decorator | Example code

Python staticmethod decorator is used to defines a static method in the class. It’s a built-in decorator. Static methods that are bound to a class rather than its object.

Python staticmethod decorator Example

Simple example code Define Static Method using a decorator. The static method can be called using the ClassName.MethodName() or object.MethodName(), as shown below.

class Student:
    name = '007'  # class attribute

    def __init__(self):
        self.age = 100  # instance attribute

    @staticmethod
    def show():
        print('Student Class')


Student.show()

Output:

Python staticmethod decorator

Note: The static method cannot access the class attributes or instance attributes.

Do comment if you have any doubts and suggestions on this Python decorator code.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.