By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 2,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Overloading the "and" operator

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Fredrik Bertilsson wrote:
I am trying to overload the "and" operatior, but my __and__ method is
never called.


__and__ overloads the "&" operator. The "and" keyword cannot be overloaded.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.