Static methods in Python
Introduction
This is a continuation of the article
«Classes»
from the section
«OOP in Python»
.
Here you can read about
static methods
Ordinary methods, in other words,
instance methods
you can study
here
class methods -
here
I also recommend studying the article
«Decorators in Python»
Static methods take as an argument neither an object of the class nor the class, but either nothing or something not related to the class.
Example
class Employee: def __init__(self, first, last, pay): self.first = first self.last = last self.pay = pay self.email = first + '.' + last + '@company.com' @staticmethod def is_workday(day): if day.weekday() == 5 or day.weekday() == 6: return False return True print(Employee.is_workday(my_date))
python staticmethod_example.py
True
A static method can be added to a completely different class without changes and it will work.
Article author: Andrei Olegovich