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
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()
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)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.