Reputation: 15434
I am trying to implement contains such that it can be used for any of several attributes in my python object. I was able to successfully implement "==" and most of the other comparison operators, but "in" is giving me problems:
import operator
class Comparator:
def __init__(self,fieldName,compareToValue,my_operator):
self.op = my_operator
self.field = fieldName
self.comparedTo = compareToValue
def __call__(self,row):
my_row_val = getattr(row,self.field)
return self.op(my_row_val,self.comparedTo)
class Row:
class RowItem:
def __init__(self,name):
self.name = name
def __eq__(self,other):
return Comparator(self.name,other,operator.eq)
def __contains__(self,other):
return Comparator(self.name,other,operator.contains)
val1 = RowItem("val1")
val2 = RowItem("val2")
val3 = RowItem("val3")
val4 = RowItem("val4")
def __init__(self, val1, val2, val3, val4):
self.val1 = val1
self.val2 = val2
self.val3 = val3
self.val4 = val4
def __str__(self):
return str([self.val1,self.val2,self.val3,self.val4])
def __repr__(self):
return str(self)
class MyTable:
def __init__(self,rows):
self.rows = rows
def filter(self,condition):
for row in self.rows:
if condition(row):
yield row
rows = [Row(1,2,3,"hello"),Row(1,2,7,"cat"),Row(1,2,3,"hi"),Row(7,7,7,"foo")]
mytable = MyTable(rows)
# the line below works fine!
print list(mytable.filter(Row.val3 == 7))
# this line below does not work
print list(mytable.filter("h" in Row.val4))
# TypeError: 'bool' object is not callable
# this line also does not work
print list(mytable.filter(Row.val4 in "hello world"))
# TypeError: 'in <string>' requires string as left operand, not instance
Upvotes: 0
Views: 563
Reputation: 15434
Thanks to Kevin for answering this in the comments. The issue was that in
(the __contains__()
method) coerces the result to a boolean unlike the other logical comparison operators (__lt__()
, __eq__()
, and others).
It seems like the reason for this is mostly backwards compatibility. More information here: https://mail.python.org/pipermail/python-dev/2013-July/127297.html
One way around this is to create a new method (for example, contains_):
try something like this (this is a bad example, because contains would work in this code :
import operator
class Comparator:
def __init__(self,fieldName,compareToValue,my_operator):
self.op = my_operator
self.field = fieldName
self.comparedTo = compareToValue
def __call__(self,row):
my_row_val = getattr(row,self.field)
return self.op(my_row_val,self.comparedTo)
class Row:
class RowItem:
def __init__(self,name):
self.name = name
def __eq__(self,other):
return Comparator(self.name,other,operator.eq)
def contains_(self,other):
return Comparator(self.name,other,operator.contains)
val1 = RowItem("val1")
val2 = RowItem("val2")
val3 = RowItem("val3")
val4 = RowItem("val4")
def __init__(self, val1, val2, val3, val4):
self.val1 = val1
self.val2 = val2
self.val3 = val3
self.val4 = val4
def __str__(self):
return str([self.val1,self.val2,self.val3,self.val4])
def __repr__(self):
return str(self)
instead of:
def __contains__(self,other):
return Comparator(self.name,other,operator.contains)
Of course, when trying to perform "in", you would need to do something like this:
print list(mytable.filter(Row.val4.contains_("h"))) #new way to call in (__contains__)
instead of:
print list(mytable.filter(Row.val4.__contains__("h"))) #old broken way to call in (__contains__)
Upvotes: 1
Reputation: 7063
filter
you have to pass a callable, not a booleanrow_obj.val4
is an instance of the class RowItem
, and not a string as expected by the __contains__
method of the string
classUpvotes: 1