Статические методы в Python

Contents
Введение
Пример
Похожие статьи

Введение

Это продолжение статьи «Классы» из раздела «ООП в Python» .

Здесь вы можете прочитаь про статические методы

Обычные, иначе говоря, instance методы вы можете изучить здесь класс методы - здесь

Рекомендую также изучить статью «Декораторы в Python»

Статические методы принимают в качестве аргумента не объект класса и не класс а либо ничего, либо что-то не связанное с классом.

Пример

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

Статический метод можно добавить в совершенно другой класс без изменений и он будет работать.

Banner Image
Related Articles
OOP in Python
Classes
Methods
class variables
class methods
Static Methods
Inheritance
Special Methods
property decorator
Python
Functions
super()

Search on this site

Subscribe to @aofeed channel for updates

Visit Channel

@aofeed

Feedback and Questions in Telegram

@aofeedchat