Overloading the "and" operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fredrik Bertilsson

    #1

    Overloading the "and" operator

    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.to String() + ") and (" +
    self.rFilter.to String() + ")"


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

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

    /Fredrik
  • Robert Kern

    #2
    Re: Overloading the "and&qu ot; operator

    Fredrik Bertilsson wrote:[color=blue]
    > I am trying to overload the "and" operatior, but my __and__ method is
    > never called.[/color]

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

    --
    Robert Kern
    rkern@ucsd.edu

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    Working...