[Chicago] Ode to <>

Brian Ray bray at sent.com
Fri Sep 1 16:21:56 CEST 2006


On Aug 31, 2006, at 6:03 PM, Ian Bicking wrote:

> For anyone following along but unsure about these magic methods:
> http://docs.python.org/ref/specialnames.html

These special method names seem to be Python's way of operator  
overloading, indeed.

Still, your not defining an operator, your picking or adding behavior  
to one already existing.

class A:
    def __init__(self,title):
       self.title = title
    def __add__(self,rhs):
       return "%s %s" % (rhs.title, self.title)

if __name__ == "__main__":
    mine = A("Hi")
    your = A("Ian")
    print mine + your


Neverthehow, this may still be what I am looking for. Although, I  
could not get this to work:

class A:
    def __init__(self,title):
       self.title = title
    def __cmp__(self,rhs):
       return rhs.title == self.title

if __name__ == "__main__":
    mine = A("Hi")
    your = A("Hi")
    print mine == your

Maybe the == inside the method is being overwritten, as well. Or  
maybe I have this all wrong.

-- bhr


More information about the Chicago mailing list