I'm trying to overload the in
operator for a class to return a non-bool object, but it seems to cast anyway. Here is my use case:
class Dataset(object):
def __init__(self):
self._filters = []
def filter(self, f):
self._filters.append(f)
return self
class EqualFilter(object):
def __init__(self, field, val):
...
class SubsetFilter(object):
def __init__(self, field, vals):
...
class FilterBuilder(object):
def __init__(self, field):
self._field = field
def __eq__(self, val):
return EqualFilter(self._field, val)
def __contains__(self, vals):
return SubsetFilter(self._field, vals)
veggie = FilterBuilder('veggie')
fruit = FilterBuilder('fruit')
ds = Dataset().filter(veggie == 'carrot').filter(fruit in ['apple', 'orange'])
At the end of the code, ds
contains an EqualFilter
for veggie == 'carrot'
, and True
for fruit in ['apple', 'orange']
. Is there any way for ds to end up with a SubsetFilter
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…