Skip to content

Python staticmethod decorator | Example code

  • by

Python staticmethod decorator is used to define a static method in the class. It’s a built-in decorator. Static methods 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 or suggestions on this Python decorator code.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading