myint.py

#!/usr/bin/env python
#
# Author:  Yotam Medini  [email protected] -- Created: 2006/September/15
#
# Example of operator overloading
#

import sys


class MyInt:

    def __init__(self, n=0):
        self.n = n

    def __str__(self):
        return "<MyInt(%d)>" % self.n

    def __add__(self, other):
        return MyInt(self.n + other.n)


m2 = MyInt(2)
m3 = MyInt(3)
m5 = m2 + m3  # calls:  m2.__add__(m3)
sys.stdout.write("%s + %s = %s\n" % (m2, m3, m5))
sys.exit(0)

Generated by GNU enscript 1.6.4.