- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Python staticmethod decorator: @staticmethod
The static method decorator, or @staticmethod, is used to define methods inside a class as static methods. For example:
class fresherearth: @staticmethod def statfun(x): print("Value of x:", x) fresherearth.statfun("Hey!")
Here is its output:
Value of x: Hey!
Note: Static methods do not have access to what the class is. The static method is just a function, without having access to the object of the class (where the static method is defined) or its internals. The differentiation between these two is provided in class methods versus static methods.
Note: Also, there is no "self" or "cls" parameter for the static method. When we need to define a normal static method inside a class that can be called directly using the class, we need the @staticmethod decorator to do the task.
Python @staticmethod decorator syntax
The syntax of the @staticmethod decorator in Python is given below.
@staticmethod def myfun(arg1, arg2, arg3, ..., argN): # definition code goes here
Python @staticmethod decorator example
Here is a simple example of the @staticmethod decorator in Python. This program employs the @staticmethod syntax to define static methods within the "fresherearth" class:
class fresherearth: @staticmethod def myfun(a, b, c, s): print("The value of a:", a) print("The value of b:", b) print("The value of c:", c) print("The value of s:", s) fresherearth.myfun(100, 200, 300, "Python is Fun!")
The sample output produced by this Python program is shown in the snapshot given below:
Advantages of the @staticmethod decorator in Python
- Code Organization: The "@staticmethod" decorator aids in the logical organization of code. It distinguishes between methods that need access to instance-specific data and those that operate on class-level data.
- Code reuse: "@staticmethod" methods from one class can be used by different instances of that class without having to duplicate or modify them. This can reduce the amount of code that needs to be written and save time.
- Performance Gains: "@staticmethod" methods can be called without first creating a class instance. When the class is frequently instantiated, this can lead to better performance.
Disadvantages of the @staticmethod decorator in Python
- Limited Access: Methods with the annotation "@staticmethod" are unable to access instance-specific data or methods. This may reduce their usefulness in particular circumstances.
- Testability: Because instance methods have access to instance-specific data, "@staticmethod" methods may be more challenging to test than instance methods. This may make it more difficult to create thorough tests for the class.
- Limited Inheritance: Child classes cannot override "@staticmethod" methods. The class hierarchy's flexibility may be constrained as a result.
« Previous Tutorial Next Tutorial »