Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
@dataclass class MultiFilter: functions: list = field(default_factory=list) mode: str = 'judge_any' iterable: list = field(default_factory=list, init=False) def __post_init__(self): self.pos = len(self.functions) // 2 def judge_half(self, v): return sum(map(lambda f: f(v), self.functions)) > self.pos def judge_any(self, v): return any(map(lambda f: f(v), self.functions)) def judge_all(self, v): return all(map(lambda f: f(v), self.functions)) def __call__(self, v, mode='judge_any'): return getattr(self, mode or self.mode)(v) def __iter__(self): return (v for v in self.iterable if getattr(self, self.mode)(v)) custom_filter = MultiFilter([ lambda x: x % 2 != 0, lambda x: x > 10 ]) print(list(filter(custom_filter, range(20)))) > [1, 3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19] custom_filter.iterable = range(20) print(list(custom_filter)) > [1, 3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19]