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

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

Введение

Это продолжение статьи «Классы» из раздела «ООП в 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

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

Article author: Andrei Olegovich

Похожие статьи
ООП в Python
Классы
Методы
class variables
class methods
Статические методы
Наследование
Специальные методы
dataclass
__slots__
Декоратор property
super()

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: