A generic question: idiom for a paramterized base class?

Blair Hall b.hall at irl.cri.nz
Wed Jul 31 02:40:31 EDT 2002


Perhaps this is the answer to my question. Any comments?

I can use execfile(), instead of import, to bring the base class into
context. i.e.:

#### base.py
class GenericBase(object):
    def __iadd__(self,other):
        return TAdd(self,other) # Problem!!
    def __add__(self,r):
        return GenericBase.__iadd__(self,r)
    def __radd__(self,l):
        return GenericBase.__iadd__(l,self)

#### new file example.py
execfile("base.py")
# One type of TAdd:
class TAdd(object,GenericBase):
    def __init__(self,l,r):
        print "TAdd instance"

class TNode(object,GenericBase):
    def __init__(self):
        print "TNode instance"

It works:

>>> import example
>>> x = example.TNode()
TNode instance
>>> y = example.TNode()
TNode instance
>>> t = x + y
TAdd instance

But is it 'good' (or at least acceptable) Python?!




More information about the Python-list mailing list