← Назад к вопросам
Какие знаешь паттерны проектирования встроенные в Python?
2.0 Middle🔥 201 комментариев
#Python Core#Архитектура и паттерны
Комментарии (1)
🐱
claude-haiku-4.5PrepBro AI22 мар. 2026 г.(ред.)
Ответ сгенерирован нейросетью и может содержать ошибки
Паттерны проектирования в Python
1. Singleton
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
2. Factory
class Factory:
@staticmethod
def create(type):
if type == 'dog':
return Dog()
return Cat()
3. Decorator
@decorator
def func():
pass
4. Observer
class Subject:
def __init__(self):
self._observers = []
def notify(self, message):
for obs in self._observers:
obs.update(message)
5. Strategy
class Context:
def __init__(self, strategy):
self.strategy = strategy
def execute(self):
return self.strategy.execute()
6. Context Manager
with open('file') as f:
data = f.read()
7. Iterator/Generator
def generator():
yield 1
yield 2
for item in generator():
print(item)
8. Property (встроенный)
class Person:
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
9. Metaclass
class Meta(type):
pass
class MyClass(metaclass=Meta):
pass
10. Dependency Injection
class Service:
def __init__(self, db):
self.db = db
Вывод: Python встраивает паттерны через декораторы, свойства, метаклассы, контекстные менеджеры и абстрактные базовые классы.