There can be some functionality that relates to the class, but does not require any instance(s) to do some work, static methods can be used in such cases. A static method is a method which is bound to the class and not the object of the class. It can’t access or modify class state. It is present in a class because it makes sense for the method to be present in class. A static method does not receive an implicit first argument.
Syntax: class C(object): @staticmethod def fun(arg1, arg2, ...): ... Returns: a static method for function fun.
When function decorated with
@staticmethod is called, we don’t pass an instance of the class to it as it is normally done with methods. It means that the function is put inside the class but it cannot access the instance of that class.
The result is 3
Is person adult: False Is person adult: True
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python - Read blob object in python using wand library
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- twitter-text-python (ttp) module - Python
- Python | PRAW - Python Reddit API Wrapper
- Python | Merge Python key values to list
- MySQL-Connector-Python module in Python
- Python | Index of Non-Zero elements in Python list
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- set add() in python
- abs() in Python
- max() and min() in Python
- Python Set | pop()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.