Overloading the "and" operator

Fredrik Bertilsson fredrik_bertilsson at passagen.se
Sun Mar 13 00:33:14 EST 2005


I am trying to overload the "and" operatior, but my __and__ method is
never called. The code look like this:

class Filter:
	column = ""
	operator = ""
	value = ""

	def __init__(self, col, op, val):
		self.column = col
		self.operator = op
		self.value = val
		
	def toString(self):
		return self.column + " " + self.operator + " " + self.value
	
	def __and__(self, other):
		print "And"
		return And(self, other)
		
		
class And:
	lFilter = None
	rFilter = None

	def __init__(self, l, r):
		self.lFilter = l
		self.rFilter = r
		
	def toString(self):
		return "(" + self.lFilter.toString() + ") and (" +
self.rFilter.toString() + ")"

		
f1 = Filter("name", "=", "Kalle")
f2 = Filter("city", "=", "LA")
andFilter = (f1 and f2)
print andFilter.toString()

What is wrong with this code? The result when I run the script is
"city = LA". The AndFilter is never created.

/Fredrik



More information about the Python-list mailing list