from typing import Type class BaseClient: _data_type = None @property def is_closed(self): return False class BaseClientFactory: def _get_client(self, client_cls: Type[BaseClient]) -> BaseClient: client_prop_name = f"_{client_cls.__name__.lower()}" instance = getattr(self, client_prop_name, None) if not instance or instance.is_closed: instance = client_cls() setattr(self, client_prop_name, instance) return instance