#!/usr/bin/env python

class Numstr:

    def __init__(self, num=0, string=''):
        self.__num = num
        self.__string = string

    def update(self, num=None, string=None):
        if num != None:
            self.__num = num
        if string != None:
            self.__string = string

    def __str__(self):
        return '[%d :: %s]' % (self.__num, `self.__string`)
    __repr__ = __str__

    def __add__(self, other):
        if isinstance(other, Numstr):
            # add numbers and strings, create and return new object
            return self.__class__(self.__num + other.__num, self.__string + other.__string)
        else:
            raise TypeError, 'illegal argument type for built-in operation'

    def __radd__(self, other):
        if isinstance(other, Numstr):
            # add numbers and strings, create and return new object
            return self.__class__(other.num + self.num, other.str + self.str)
            # could also be (self.__num + other.__num, other.__string + self.__string)
        else:
            raise TypeError, 'illegal argument type for built-in operation'

    def __mul__(self, other):
        if type(other) == type(0):
            return self.__class__(self.__num * other, self.__string * other)
        else:
            raise TypeError, 'illegal argument type for built-in operation'

    def __nonzero__(self):
        return self.__num and len(self.__string)

    def __normalize_cmpval(self, cmpres):
        if cmpres < 0:
            return -1
        elif cmpres > 0:
            return 1
        else:
            return 0

    def __cmp__(self, other):
        #return 200
        nres = self.__normalize_cmpval(cmp(self.__num, other.__num))
        sres = self.__normalize_cmpval(cmp(self.__string, other.__string))

        if not (nres or sres):                # both 0
            return 0

        sum  = nres + sres

        if not sum:                        # one <, one >
            return None
        #return 10 * sum
        return sum

# no output from these lines of code, which perform the operation,
# but do not do anything with the results; import this module in
# the interpreter, then access the variables 'a' - 'f':
#
# >>> import numstr
# >>> numstr.a
# [3 :: 'foo']

a=Numstr(3, 'foo')
b=Numstr(3, 'goo')
c=Numstr(2, 'foo')
d=Numstr()
e=Numstr(0,'aaa')
f=Numstr(1)
g=Numstr(string='boo')
