A generic question: idiom for a paramterized base class?

Donnal Walter donnal at donnal.net
Wed Jul 31 15:05:19 EDT 2002


"Sean 'Shaleh' Perry" <shalehperry at attbi.com> wrote in message news:<mailman.1028067667.7081.python-list at python.org>...
> > 
> > That might do what you want.  To me, though, the whole thing seems vague
> > and maybe there's another approach that avoids some of these
> > complexities.
> > 
> 
> indeed ugly hierarchies often indicate to little or too much thought.

Does this do what you want?

##################################################
class AbstractBase(object):
    def __iadd__(self,other):
        return self.TAdd(self,other)
    def __add__(self,r):
        return AbstractBase.__iadd__(self,r)
    def __radd__(self,l):
        return AbstractBase.__iadd__(l,self)
    class TAdd(object): pass

class MyClass1(AbstractBase):
    
    # One type of TAdd:
    class TAdd(AbstractBase):
        def __init__(self,l,r):
            print "TAdd instance 1"

class MyClass2(AbstractBase):
    
    # Another type of TAdd:
    class TAdd(AbstractBase):
        def __init__(self,l,r):
            print "TAdd instance 2"
##################################################

>>> x = MyClass1()
>>> y = MyClass2()
>>> t = x + y
TAdd instance 1
>>> u = y + x
TAdd instance 2

Donnal Walter
Arkansas Children's Hospital



More information about the Python-list mailing list