If we rewrite the original class to be immutable (...as was my initial intention, but I wrote it fast), it becomes a little more clear why I'm aiming for this.
I guess using temporary variables or just having multiple filters/selectors on a single line is the way to go.
Here's probably a better example:
I guess using temporary variables or just having multiple filters/selectors on a single line is the way to go.
Here's probably a better example:
class query(object): def __init__(self, source, filters = ()): self.filters = filters self.source = source def where(self, filter): filters = self.filters + (filter, ) new_query = query(self.source, filters) return new_query def __and__(self, other): filters = self.filters + other.filters return query(self.source, filters) def get(self): all_filters = lambda x: all(func(x) for func in self.filters) return list(filter(all_filters, self.source)) things = query(range(1000)) by_9 = things.where(lambda x: x % 9 == 0) only_odd = things.where(lambda x: x % 2 != 0) further_filtered = only_odd.where(lambda x: x % 3 == 0).where(lambda x: x % 7 == 0) print((further_filtered & by_9).get()) # [63, 189, 315, 441, 567, 693, 819, 945]