- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- 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 & 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 & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python Send E-mail
- Python Event Handling
- Python Keywords
- Python All 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 Test
- Python Online Test
- Give Online Test
- All Test List
Python staticmethod Decorator - @staticmethod
The static method decorator or @staticmethod is used to define methods inside a class as static methods. For example:
class CodesCracker: @staticmethod def statfun(x): print("Value of x:", x) CodesCracker.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) and its internals. The differentiation between these two is provided in class method Vs static method in Python.
Note - Also there is no any self or cls parameter for static method. When we need to define a normal static method inside a class, that can call directly using the class, we need @staticmethod decorator to do the task.
Python @staticmethod Decorator Syntax
The syntax of @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 @staticmethod decorator in Python. This program uses the @staticmethod to define static methods inside a class named CodesCracker:
class CodesCracker: @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) CodesCracker.myfun(100, 200, 300, "Python is Fun!")
The sample output produced by this Python program, is shown in the snapshot given below:
« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube